1212from selenium import webdriver
1313from selenium .webdriver .common .by import By
1414from selenium .webdriver .support .ui import WebDriverWait
15- from selenium .webdriver .support import expected_conditions
15+ from selenium .webdriver .support import expected_conditions as EC
16+
17+ from time import sleep
1618
1719URL = 'https://www.tmall.com'
1820
@@ -24,23 +26,43 @@ def setUp(self):
2426 self .driver .implicitly_wait (5 )
2527
2628 def test_wait (self ):
27- login_line = WebDriverWait (self .driver , 30 ).until (
28- expected_conditions .visibility_of_element_located ((By .LINK_TEXT , '请登录' )))
29+ login_line = WebDriverWait (self .driver , 30 ).until (EC .visibility_of_element_located ((By .LINK_TEXT , '请登录' )))
2930 self .assertEqual ('sn-login' , login_line .get_attribute ('class' ))
3031
32+
3133 # login_line.click()
3234 # self.driver.implicitly_wait(20)
3335
34- # def test_element_to_be_enalble(self):
35- # self.driver.find_element_by_link_text('请登录').click()
36- # free_sign_up = WebDriverWait(self.driver, 30).until(
37- # expected_conditions.presence_of_element_located((By.XPATH, '//*[@id="J_QRCodeLogin"]/div[5]/a[2]')))
38- # free_sign_up.click()
39- #
40- # agree_btn = WebDriverWait(self.driver, 20).until(
41- # expected_conditions.element_to_be_clickable((By.LINK_TEXT, '同意协议')))
42- # agree_btn.click()
43- # WebDriverWait(self.driver, 20).until(expected_conditions.title_contains('天猫注册'))
36+ def test_element_to_be_enalble (self ):
37+ self .driver .find_element_by_link_text ('请登录' ).click ()
38+
39+ # 注意,有关用户登录,一定注意要先 switch_to.frame(iframe)
40+ iframe = self .driver .find_element_by_tag_name ("iframe" )
41+ WebDriverWait (self .driver , 30 ).until (EC .frame_to_be_available_and_switch_to_it (iframe ))
42+
43+ free_sign_up = WebDriverWait (self .driver , 30 ).until (
44+ # EC.presence_of_element_located((By.XPATH, '//*[@id="J_QRCodeLogin"]/div[5]/a[2]')))
45+ EC .visibility_of_element_located ((By .LINK_TEXT , "免费注册" )))
46+ free_sign_up .click ()
47+
48+ sleep (6 )
49+
50+ # js_monitor = 'document.getElementById("J_AgreementDialog").style.display = "none"'
51+ # self.driver.execute_script(js_monitor)
52+
53+ # 难点, 无法定位到同意协议窗体。
54+ # self.driver.switch_to_alert().dismiss()
55+ iframe = self .driver .find_element_by_tag_name ("iframe" )
56+ WebDriverWait (self .driver , 30 ).until (EC .frame_to_be_available_and_switch_to_it (iframe ))
57+
58+ agree_btn = self .driver .find_element_by_xpath ('//*[@id="J_AgreementBtn"]' )
59+ # agree_btn = WebDriverWait(self.driver, 20).until(
60+ # EC.visibility_of_element_located((By.XPATH, '//*[@id="J_AgreementBtn"]')))
61+
62+ agree_btn .click ()
63+
64+ WebDriverWait (self .driver , 20 ).until (EC .title_contains ('天猫注册' ))
65+ self .assertEqual ("天猫注册" , self .driver .title )
4466
4567 def test_custom_coditions (self ):
4668 self .driver .get (URL )
@@ -54,7 +76,7 @@ def tearDown(self):
5476
5577 # 可以代替lambda 表达式, return True
5678 def check_q (self ):
57- if self .driver .find_element_by_name ('q' ).get_attribute ('title' ) == '请输入搜索文字' :
79+ if self .driver .find_element_by_name ('q' ).get_attribute ('title' ) == '请输入搜索文字' :
5880 return True
5981
6082 else :
0 commit comments