【软件测试7】web自动化测试——12306购票实战

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:

元素找到了,但是没有加载出来

解决方案:添加固定等待 time.sleep(2)

element not interactable :

元素找到了但是不能操作

**原因1:**被遮住了

解决方案: 关闭遮盖的页面

**原因2:**没有显示出来

解决方案: 让它显示

六、代码:

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options

#去掉自动化标识
option=Options()
option.add_experimental_option('excludeSwitches',['enale-automation'])
option.add_argument('--disable-blink-features=AutomationControlled')
#打开Chrome
driver=webdriver.Chrome(options=option)
#添加隐式等待
driver.implicitly_wait(10)
# 访问登录页
driver.get('https://kyfw.12306.cn/otn/view/index.html')
#输入用户名
driver.find_element('xpath','//*[@id="J-userName"]').send_keys('18295570687')
#输入密码
driver.find_element('xpath','//*[@id="J-password"]').send_keys('xuhui1624')
#点击登录
driver.find_element('xpath','//*[@id="J-login"]').click()
#验证码破解:拖住滑块,滑到最右边
#找到滑块
ele=driver.find_element('xpath','//*[@id="nc_1__scale_text"]/span')
#按住滑块
act=ActionChains(driver)#鼠标操作
act.click_and_hold(ele)
#拖动到最右边,横向拖动520,大于滑块的长度
act.move_by_offset(520,0)
#使鼠标操作生效
act.perform()
#鼠标移动到车票上
tiket=driver.find_element('xpath','//*[@id="J-chepiao"]/a')
act.move_to_element(tiket)
act.perform()
#点击单程票
driver.find_element('xpath','//*[@id="megamenu-3"]/div[1]/ul/li[1]/a').click()
#输入出发地
driver.find_element('xpath','//*[@id="fromStationText"]').click()
driver.find_element('xpath','//*[@id="fromStationText"]').send_keys('杭州')
driver.find_element('xpath','//*[@id="citem_1"]').click()
#输入目的地
driver.find_element('xpath','//*[@id="toStationText"]').click()
driver.find_element('xpath','//*[@id="toStationText"]').send_keys('成都')
driver.find_element('xpath','//*[@id="citem_0"]/span[1]').click()
#输入出发日期
driver.find_element('xpath','//*[@id="train_date"]')
driver.find_element('xpath','//*[@id="train_date"]').clear()
driver.find_element('xpath','//*[@id="train_date"]').send_keys('2023-4-28')
#点击查询
driver.find_element('xpath','//*[@id="query_ticket"]').click()
#找到指定车次的预定按钮
driver.find_element('xpath','//*[@id="ticket_5l000G218911_04_23"]/td[13]/a').click()


  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值