当前位置:网站首页>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')
边栏推荐
- Large neural networks may be beginning to realize: the chief scientist of openai leads to controversy, and everyone quarrels
- Blender camera surround motion, animation rendering, video synthesis
- Configuration programmée du générateur de plantes du moteur illusoire UE - - Comment générer rapidement une grande forêt
- Ue5 - AI pursuit (blueprint, behavior tree)
- Project practice, redis cluster technology learning (11)
- How to achieve the top progress bar effect in background management projects
- Remember the use of add method once
- How to judge the quality of primary market projects when the market is depressed?
- Matlab generates DSP program -- official routine learning (6)
- 职业规划和发展
猜你喜欢

A model can do two things: image annotation and image reading Q & A. VQA accuracy is close to human level | demo can be played

MySQL transaction

渗透测试的介绍和防范

Blender camera surround motion, animation rendering, video synthesis

Ue5 - AI pursuit (blueprint, behavior tree)

The latest progress and development trend of 2022 intelligent voice technology

Blender海洋制作

【虚幻】武器插槽:拾取武器

2837xd 代碼生成——補充(1)

Unreal material editor foundation - how to connect a basic material
随机推荐
Operator exercises
ICLR 2022: how does AI recognize "things I haven't seen"?
[200 Shengxin literatures] 95 multiomics exploration TNBC
阿里云SLS日志服务
Tee command usage example
滲透測試的介紹和防範
This monitoring system makes workers tremble: turnover intention and fishing can be monitored. After the dispute, the product page has 404
渗透测试的介绍和防范
Summary of demand R & D process nodes and key outputs
Blender stone carving
Data insertion in C language
What is call / cc- What is call/cc?
Blender multi lens (multi stand) switching
XA Transaction SQL Statements
Applet development summary
This article takes you to learn in detail what is fiber to home FTTH
Spatial interpretation | comprehensive analysis of spatial structure of primary liver cancer
Alibaba cloud Prometheus monitoring service
Matlab生成dsp程序——官方例程学习(6)
高考那些事