原始代码:

 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这个框架与窗口都是一个囊括其他元素的集合或者说模块,里面的东西是被封装的,因此想要定位到里面的元素就需要先切换到这个款架里面,方可定位。

解决:在python+selenium账号脚本登陆时,使用qq账号密码登陆百度账号出现的问题相关推荐

  1. 登陆时按enter键实现登陆

    登陆时按enter键实现登陆 首先需要在页面body标签中添加一个事件onkeydown() <body "keydown()" > 写方法 function keyd ...

  2. PHP实现QQ、微博、百度账号的连通(二次)

    PHP实现的QQ互联.新浪微博.百度账号的账户打通. <?php $oauth = new oauth; $login = strlen($oauth->get('login')) < ...

  3. Python+Selenium自动化测试脚本(入门)

    前言 最近在学习Python+selenium自动化测试,计划把公司已有一套自动化测试框架(UIAutomation+C#)用Python+Selenium框架实现一遍.先拿了一个csdn的登录页面练 ...

  4. python+selenium+pytesseract实现自动识别简单验证码,并且自动登陆网站

    文章目录 功能介绍 首先安装需要的库 编写代码 功能介绍 本文章实现自动识别验证码并且自动登陆网站,完全不需要人工操作 Python版本:3.6 Selenium:selenium 是一个用于 Web ...

  5. linux中添加一个用户名和密码,uClinux中添加Telnetd登陆时的用户名和密码

    方法1: 编译Kernel的时,make menuconfig,打开Tinglogin,选择adduser(主要用于创建用户的password),addgroup(否则编译会报错,adduser程序中 ...

  6. python模拟登录密码加密_有关爬虫模拟登陆时,处理用户名密码加密问题的方法...

    之前爬一家网站的数据,需要登陆之后才能进行:所以当时使用selenium偷鸡了一波: 现在我想尝试用requests方法,结果在登录时,就遇到了用户名密码加密问题 查了下登陆有关的js,找到了加密的方 ...

  7. python+selenium自动化脚本例子

    import time from selenium import webdriver from selenium.webdriver.common.keys import Keys # 键盘 from ...

  8. Python+Selenium自动化脚本实现列表信息的多列内容获取

    自动化脚本实现列表信息的多列内容获取(列数目可变) 取出多列信息后显示为一个列表,根据列数目进行列表的拆分,最终展示为多个子列表 # 获取列表数据多列内容def get_list_multicolum ...

  9. 完美解决:Python在安装XXX包时,报错拒绝访问的错误,PermissionError: [WinError 5] 拒绝访问。: ‘d:\\anaconda3\\lib\\site-package

    1.环境:我是anaconda3(也即Python3),windows 10. 2.解决方案:最最最简单好用的解决方法就是将我们常规写的                     pip install ...

  10. Python+Selenium - 练习:打开京东->QQ登录->修改个人信息(含鼠标悬停后选中二级菜单进行点击)

    举例题目:打开京东->QQ登录->修改个人信息 覆盖知识点:         1.id.name.link定位         2.xpath.css定位及相对路径定位         3 ...

最新文章

  1. Python 工程管理及 virtualenv 的迁移
  2. POJ1144:Network(无向连通图求割点)
  3. 斐波那契 非递归写法 非递归实现
  4. mysql数据库存储数据的过程_[数据库]MySql存储过程总结
  5. 从一个小demo开始,体验“API经济”的大魅力
  6. PHP_MySQL笔试题目一
  7. ZOJ-1101-Gamblers
  8. UVA208Firetruck 消防车(图的路径搜索)
  9. PHP实现加密的几种方式介绍
  10. Shell 命令大全Xhell入门
  11. 求序列第K大算法总结
  12. 计算机专业买win,新买的电脑是win10系统,有的人却费尽心思重装成win7,为什么呢...
  13. phpstorm增加swoole自动提示
  14. 这两种方法能使PDF不能被复制和修改
  15. HTML5实习手机端浏览器拍照和本地上传
  16. Myscript 转换
  17. 树莓派4B+Intel神经计算棒(Stick2)+YoloV5可行性考察报告
  18. taobao.trades.sold.increment.get( 淘宝店铺订单接口,淘宝店铺订单交易数据接口,淘宝店铺订单解密接口,淘宝店铺订单解密提额接口)代码对接教程
  19. 【机器学习基础】Scipy(科学计算库) 手把手手把手
  20. ECS运维神器 之 阿里云云助手

热门文章

  1. 计算机专业毕设都需要哪些准备,计算机专业本科毕设指南.ppt
  2. 医院项目-预约挂号-第五部分
  3. Sa-token SSO单点登录机制【源码】
  4. IJCAI 2018 阿里妈妈广告预测算法大赛
  5. 机械转码,学习中讲讲感受
  6. performSelector详解
  7. 重装服务器显示bootmgr缺失怎么办,修复“BOOTMGR缺失”的4种方法
  8. 论文笔记:传统CV和深度学习方法的比较
  9. 手机的几大隐密功能--转
  10. 赵英俊的重唱计划,我看到了音乐人的倔强