web自动化测试——12306购票实战
一、自动化购票流程
- 登录
- 进入购票
- 填写信息
- 选择车次
- 预定
- 选择购票人
二、自动化环境配置
-
软件环境:Python+selenium
-
Python安装:Python+Pycharm安装
-
**selenium安装:**pip install selenium
下载地址https://registry.npmmirror.com/binary.html?path=selenium/&spm=a2c6h.24755359.0.0.6d446e51uGlZuC
-
**webdriver配置:**不同浏览器有不同的配置
Chrome浏览器:Chromedriver下载要和浏览器版本一致
下载地址:https://registry.npmmirror.com/binary.html?path=selenium/&spm=a2c6h.24755359.0.0.6d446e51Tr7MgI
解压后拷贝到Python安装根路径之下
-
**检查环境:**写一个简单的demo打开浏览器来检测是否配置好环境
三、web自动化基本原理:
把手动需要的操作全部用Python实现。
web自动化两个步骤:
-
找元素 :copy xpath
driver.find_element(‘xpath’,‘//*[@id=“J-userName”]’)
-
操作 :click,send_keys,clear(点击,输入,清空)
例如:输入用户名,运行后会自动输入123456
driver.find_element(‘xpath’,‘//*[@id=“J-userName”]’).send_keys(‘123456’)
输入密码:
driver.find_element(‘xpath’,‘//*[@id=“J-password”]’).send_keys(‘nicai’)
点击登录:
driver.find_element(‘xpath’,‘//*[@id=“J-login”]’).click()
四、验证码破解
按住滑块,拖动到最右边
关键技术1:
from selenium.webdriver import ActionChains
act=ActionChains(driver)*#*鼠标操作
ActionChains库:在刚打开的浏览器上面执行鼠标操作
关键技术2:
自动化的时候,怎么绕过防刷验证
进行配置,在打开页面之前,去掉页面上的自动化标识
目的:消除自动化与手动操作的差别
*#去掉自动化标识
*option=Options()
option.add_experimental_option(‘excludeSwitches’,[‘enale-automation’])
option.add_argument(‘–disable-blink-features=AutomationControlled’**)
五、报错
no such element,没有找到滑块这个元素,
报错原因: 弹出滑块需要时间,马上找找不到
解决方案 :在打开页面之后添加隐式等待,大大增强自动化稳定性
隐式等待特点:
- 所有找元素都会触发,
- 每隔一秒种找一下元素
- 直到配置时间到了还没找到就报错
- 如果中途找到了就继续往下执行
element is not attached to the page document:
元素找到了,但是没有加载出来
解