原始代码:
def test_loginBtn(self):
# 定位并点击登录按钮
log.info('定位并点击登录按钮')
self.driver.find_element_by_xpath('//*[@id="u1"]/a[7]').click()
log.info('点击登录按钮')
time.sleep(3)
self.driver.find_element_by_id('TANGRAM__PSP_10__footerULoginBtn').click()
time.sleep(2)
log.info("点击qq账号登陆")
self.driver.find_element_by_xpath('//*[@id="pass_phoenix_btn"]/ul/li[1]/a').click()
log.info("跳转到qq登陆界面")
time.sleep(2)
windows=self.driver.window_handles # 此行代码用来新窗口
self.driver.switch_to.window(windows[1])
time.sleep(5)
log.info('定位qq账号')
self.driver.find_element_by_id('ptlogin_iframe').click()
log.info('qq窗口最大化')
self.driver.maximize_window()
time.sleep(2)
log.info('点击账号密码登陆')
self.driver.find_element_by_id('switcher_plogin').click()
log.info("点击qq登陆按钮")
self.driver.find_element_by_id('web_login').click()
log.info('输入用户名和密码')
self.driver.find_element_by_id('u').send_keys('*********@qq.com')
time.sleep(5)
self.driver.find_element_by_id('p').send_keys('password')
time.sleep(5)
log.info('点击授权并登录按钮')
self.driver.find_element_by_id('login_button').click()
time.sleep(2)
出现的BUG提示:
Traceback (most recent call last):
File "D:\python\p\lib\unittest\case.py", line 59, in testPartExecutor yield
File "D:\python\p\lib\unittest\case.py", line 615, in run testMethod()
File "F:\unittest\pandabus_unittest\pandabus_unittest\testcase\test_login_baidu.py", line 71, in test_loginBtn element = self.driver.find_element_by_xpath('//*[@id="switcher_plogin"]')
File "D:\python\p\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath return self.find_element(by=By.XPATH, value=xpath)
File "D:\python\p\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element 'value': value})['value']
File "D:\python\p\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute self.error_handler.check_response(response)
File "D:\python\p\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="switcher_plogin"]"} (Session info: chrome=74.0.3729.108) (Driver info: chromedriver=2.43.600210 (68dcf5eebde37173d4027fa8635e332711d2874a),platform=Windows NT 6.1.7601 SP1 x86_64)
分析原因:
解决问题: 修改后的代码
def test_loginBtn(self):
# 定位并点击登录按钮
log.info('定位并点击登录按钮')
self.driver.find_element_by_xpath('//*[@id="u1"]/a[7]').click()
log.info('点击登录按钮')
time.sleep(3)
self.driver.find_element_by_id('TANGRAM__PSP_10__footerULoginBtn').click()
time.sleep(2)
log.info("点击qq账号登陆")
self.driver.find_element_by_xpath('//*[@id="pass_phoenix_btn"]/ul/li[1]/a').click()
log.info("跳转到qq登陆界面")
time.sleep(2)
windows=self.driver.window_handles # 此行代码用来新窗口
self.driver.switch_to.window(windows[1])
time.sleep(5)
log.info('定位qq账号')
self.driver.find_element_by_id('ptlogin_iframe').click()
log.info('qq窗口最大化')
self.driver.maximize_window()
time.sleep(2)
log.info('定位到鼠标移动到目标登录按钮')
# 切换至账户密码框
self.driver.switch_to.frame('ptlogin_iframe')
element = self.driver.find_element_by_xpath('//*[@id="switcher_plogin"]')
ActionChains(self.driver).move_to_element(element).perform()
log.info('点击账号密码登陆')
self.driver.find_element_by_id('switcher_plogin').click()
log.info("点击qq登陆按钮")
self.driver.find_element_by_id('web_login').click()
log.info('输入用户名和密码')
self.driver.find_element_by_id('u').send_keys('*********@qq.com')
time.sleep(5)
self.driver.find_element_by_id('p').send_keys('password')
time.sleep(5)
log.info('点击授权并登录按钮')
self.driver.find_element_by_id('login_button').click()
time.sleep(2)
self.driver.switch_to_frame('ptlogin_iframe')有的人可能是加入【browser.switch_to.frame('ptlogin_iframe')】本质上都一样,这一句,是因为账户输入登陆在一个子iframe里面的,如果直接定位里面的元素是定位不到的,所以需要先切换到这个子iFrame,好像这是一个js框架,也就是切换到这个框架。
思考:
我们知道自动化测试在一个页面打开一个或多个窗口时,想要定位到其他窗口时,才会用到切换这个框架,为何同一个页面内的元素也需要切换呢? 我的理解是,iframe这个框架与窗口都是一个囊括其他元素的集合或者说模块,里面的东西是被封装的,因此想要定位到里面的元素就需要先切换到这个款架里面,方可定位。