当前位置:网站首页>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')
边栏推荐
- Following nym, the new project Galaxy token announced by coinlist is gal
- 【Lua】常见知识点汇总(包含常见面试考点)
- Matlab生成dsp程序——官方例程学习(6)
- Project practice, redis cluster technology learning (6)
- Commutateur Multi - lentilles Blender
- Introduction to go language
- VLAN experiment
- Skywalking theory and Practice
- 2837xd code generation module learning (4) -- idle_ task、Simulink Coder
- UE4 night lighting notes
猜你喜欢
Blender ocean production
2837xd代码生成模块学习(4)——idle_task、Simulink Coder
Matlab生成dsp程序——官方例程学习(6)
2837xd code generation - Supplement (3)
【UE5】动画重定向:如何将幻塔人物导入进游戏玩耍
The primary market project galaxy will conduct public offering on coinlist on February 17
How to judge the quality of primary market projects when the market is depressed?
High level application of SQL statements in MySQL database (II)
Ctrip starts mixed office. How can small and medium-sized enterprises achieve mixed office?
【虚幻】武器插槽:拾取武器
随机推荐
Blender volume fog
Introduction et prévention des essais de pénétration
2837xd 代碼生成——補充(1)
How much is it to develop a system software in Beijing, and what funds are needed to develop the software
Skywalking theory and Practice
Data insertion in C language
What wires are suitable for wiring on bread board?
go语言入门
【虚幻】过场动画笔记
2837xd code generation module learning (3) -- IIC, ECAN, SCI, watchdog, ECAP modules
Attack and defense world web advanced area unserialize3
2837xd code generation - Supplement (3)
虚幻AI蓝图基础笔记(万字整理)
[ue5] blueprint making simple mine tutorial
Blender multi lens (multi stand) switching
The latest progress and development trend of 2022 intelligent voice technology
Junit4运行mvn test 测试套件升级方案
虚幻——动画蓝图、状态机制作人物走跑跳动作
2837xd 代码生成——StateFlow(1)
Project practice, redis cluster technology learning (11)