当前位置:网站首页>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 (2) -- ADC, epwm module, timer0
- AutoCAD - layer Linetype
- Deep understanding of redis cache avalanche / cache breakdown / cache penetration
- Vscode set JSON file to format automatically after saving
- Matlab代码生成之SIL/PIL测试
- 【虚幻】武器插槽:拾取武器
- 【虚幻】过场动画笔记
- What is the relationship between realizing page watermarking and mutationobserver?
- 2837xd code generation - stateflow (2)
- UE5——AI追逐(蓝图、行为树)
猜你喜欢

Unreal material editor foundation - how to connect a basic material

2837xd code generation - Supplement (3)

How to judge the quality of primary market projects when the market is depressed?

【UE5】蓝图制作简单地雷教程

Attack and defense world web advanced area unserialize3

Junit5 支持suite的方法

Record the interesting process of using Xray for the first time

Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer

Personal experience & blog status

2837xd代码生成模块学习(2)——ADC、ePWM模块、Timer0
随机推荐
ERROR 1118 (42000): Row size too large (&gt; 8126)
Vscode auto format
This article takes you to learn in detail what is fiber to home FTTH
Blender多镜头(多机位)切换
C language strawberry
Junit4 runs MVN test test suite upgrade scheme
Commutateur Multi - lentilles Blender
Minimum number of C language
2837xd code generation module learning (4) -- idle_ task、Simulink Coder
Binary and decimal system of C language
Notes de base sur les plans illusoires d'IA (triés en 10 000 mots)
Eslint reports an error
2837xd 代码生成——StateFlow(2)
2837xd代码生成模块学习(4)——idle_task、Simulink Coder
Summary of demand R & D process nodes and key outputs
阿里云SLS日志服务
【避坑指南】使用UGUI遇到的坑:Text组件无法首行缩进两格
2837xd Code Generation - Supplement (1)
Pycaret | a few lines of code to solve machine learning modeling
Alibaba cloud SMS service