当前位置:网站首页>webUI自动化学习
webUI自动化学习
2022-07-02 06:36:00 【迷途~知返】
WebUI自动化学习
selenium基础
selenium介绍
selenium驱动浏览器
- selenium --主流版本是4.1是在2021年10月份开放下载和使用的版本 --last 3:3.141
- selenium == 4.1 -->python 3.8/python 3.9
- Selenium核心是JS Core实现的
- FireFox的插件形态,名叫Selenium的插件形态,名叫SeleniumIDE存在的
- 为了便于更多的浏览器能够使用Selenium技术,所以更新到了Selenium2版本,也就是Selenium + webdrive的技术体系,通过WebDrive来控制浏览器,通过Selenium来操作浏览器
- 随着时间推出了Selenium3的版本,这也是存在时间最长久的版本,核心就是基于Selenium + WebDrive的形态来实现所有的自动化交互
- 2021年10月份,正式推出Selenium4的版本,优化了底层结构,增加了新的擦走行为,兵器了一些不太常用的内容
- 现在SeleniumIDE有新的更新,支持各类浏览器就行脚本的录制与回放,
Selenium + WebDriver
环境搭建:
- 安装python3.7及以上版本
- pip install selenium
- 安装WebDriver:http://chromedriver.storage.googleapis.com/index.html?path=100.0.4896.60/,确保ChromeDriver.exe与浏览器的版本是一致
- 解压ChromeDriver.exe文件到Python的安装路径
- 如果担心浏览器与driver版本容易出现不匹配,可以通过使用safedriver来自动匹配你的浏览器与driver版本,安装方法pip install safedriver
- pip过程中出现read timeout error 添加国内镜像源,或者设置最大超时次数 --default-timeout=1000
- 校验环境部署成功
- 通过编码基本内容即可测试成功
from selenium import webdriver
driver = webdriver.Chrome()
python + webdriver实现UI自动化
基础操作
#coding:utf-8
""" selenium 部署后的应用 京东搜索 """
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
# 创建一个浏览器,基于驱动来启动浏览器,创建时,通过session来管理浏览器
drive = webdriver.Chrome()
# 访问京东的url:get函数要求输入的url必须是完整的内容,http://的前缀是不可以取消掉的
drive.get('http://www.jd.com')
# 再输入狂输入你所要输入的搜索内容
# 找到输入框,不要使用find_element_by_*
el = drive.find_element("id", 'key')
# 对输入框进行输入
el.send_keys('iphone13')
# 找到搜索按钮,点击
el1 = drive.find_element("xpath", '//button[@aria-label="搜索"]')
el1.click()
# 等待3s
time.sleep(10)
drive.quit()
selenium运行原理
code -> webdriver -> chrome
本质上是code–>启动chrome(webdriver.chromedrive)–>调用本地的浏览器
webdriver.chromedrive -->中间代理商
数据返回:浏览器–>webdrivedriver->code
源码解析:
# 基于driver底层实现百度搜索
# coding:utf-8
import time
from selenium.webdriver.chrome.webdriver import WebDriver
# 生成浏览器对象,通过生成的webdriver对象,去启动默认的谷歌浏览器
driver = WebDriver(executable_path="chromedriver")
# 进行网址的访问,底层代码,通过command命令去调用get方法,并执行http://www.baidu.com获取返回值
driver.execute('get', {
'url': 'http://www.baidu.com'})
# 输入框的元素定位,通过浏览器驱动来实现对find_element方法的查找
el = driver.execute('findElement', {
'using': 'css selector',
'value': '[id="kw"]'})['value']
el._execute('sendKeysToElement', {
'text': "神州十三号", 'value': ""})
el_1 = driver.execute('findElement', {
'using': 'css selector',
'value': '[id="su"]'})['value']
el_1._execute('clickElement')
time.sleep(10)
driver.execute('quit')
html基础
组成部分分为三个:
- 标签名称(TagName)
- 属性(Attribute)
- 文本(text)
常见的标签:a/div/span/i/tr/td/input/p/form…
元素的形态不要相信肉芽缩减,要基于元素的属性来决定
文本并不是每一个标签都具有的东西
<input>Text</input>
<input/>
<input></input>
selenium之8大元素定位法
find_element_by_id
相对路径的定位方法:
语法规则://[@id=“kw”],从根路径下开始查找id属性,且属性值为kw的元素
//从根路径下查找,表示任意元素,筛选条件,[]添加筛选条件,@表示基于属性进行查找,id表示属性的名称,kw表示属性的值
text() 表示基于文本定位
//[contains(text(),'‘)] 表示基于contains函数进行数据的模糊匹配
查找相邻元素
//*[contains(’‘,’')]/follow-sibling::span–查找当前元素的兄弟元素
xpath手写的方式,除去避免聚堆路径意外,还可以避免动态元素导致元素定位失败
伪元素(::before:::after::)
标签名称
selenium之窗口基本操作
# coding:utf-8
from selenium import webdriver
# 窗体最大化操作,使用chrome——op
options = webdriver.ChromeOptions()
options.add_argument('start-maximized')
# chrome浏览器配置,启动就是最大化
driver = webdriver.Chrome(options=options)
# 窗体最大化,在selenium2开始就有一个bug,调用窗体最大化时会造成driver的超时异常
# driver.maximize_window()
# 设置窗体大小尺寸
driver.set_window_size(200, 1000)
driver.get('http://39.98.138.157/shopxo/index.php')
边栏推荐
- 2837xd code generation module learning (3) -- IIC, ECAN, SCI, watchdog, ECAP modules
- Career planning and development
- How to achieve the top progress bar effect in background management projects
- Sil/pil test of matlab code generation
- VLAN experiment
- Judging right triangle in C language
- Blender石头雕刻
- Is the C language too fat
- 2837xd code generation module learning (1) -- GPIO module
- 滲透測試的介紹和防範
猜你喜欢
Commutateur Multi - lentilles Blender
Application of rxjs operator withlatestfrom in Spartacus UI of SAP e-commerce cloud
Applet development summary
2837xd code generation - Supplement (3)
2837xd代码生成模块学习(2)——ADC、ePWM模块、Timer0
Configuration programmée du générateur de plantes du moteur illusoire UE - - Comment générer rapidement une grande forêt
【虚幻4】从U3D到UE4的转型之路
Summary of demand R & D process nodes and key outputs
Aiphacode is not a substitute for programmers, but a tool for developers
Junit4运行mvn test 测试套件升级方案
随机推荐
Applet development summary
ESLint 报错
UE4夜间打光笔记
Vscode auto format
Personal experience & blog status
2837xd Code Generation - Supplement (1)
Blender多镜头(多机位)切换
Application of rxjs operator withlatestfrom in Spartacus UI of SAP e-commerce cloud
Alibaba cloud ack introduction
This monitoring system makes workers tremble: turnover intention and fishing can be monitored. After the dispute, the product page has 404
【Unity3D】制作进度条——让Image同时具有Filled和Sliced的功能
C language: making barrels
The latest progress and development trend of 2022 intelligent voice technology
Matlab生成dsp程序——官方例程学习(6)
VLAN experiment
Bookmark collection management software suspension reading and data migration between knowledge base and browser bookmarks
Blender camera surround motion, animation rendering, video synthesis
MySQL -- time zone / connector / driver type
【虚幻】武器插槽:拾取武器
Project practice, redis cluster technology learning (13)