首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

Python Selenium4.3.0(新语法) web自动化测试工具

  • 24-03-18 03:09
  • 2787
  • 6910
blog.csdn.net

1 介绍
Selenium是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。

支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,Opera,Edge等

这个工具的主要功能包括:测试与浏览器的兼容性——测试应用程序看是否能够很好得工作在不同浏览器和操作系统之上。测试系统功能——创建回归测试检验软件功能和用户需求。

中文教程:http://www.selenium.org.cn/

2 入门准备
1、安装

pip install selenium
  • 1

2、下载浏览器驱动

(1)谷歌驱动

http://chromedriver.storage.googleapis.com/index.html

# linux chrome
https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
  • 1
  • 2

(2)火狐驱动

https://github.com/mozilla/geckodriver/releases/

3、测试代码
可以指定路径,或将驱动放到当前文件夹

from selenium import webdriver

url = "https://www.baidu.com/"

# 加载驱动
# driver = webdriver.Firefox()   # Firefox浏览器
# driver = webdriver.Firefox("驱动路径")
driver = webdriver.Chrome()    # Chrome浏览器
# driver = webdriver.Ie()        # Internet Explorer浏览器
# driver = webdriver.Edge()      # Edge浏览器
# driver = webdriver.Opera()     # Opera浏览器
# driver = webdriver.PhantomJS()   # PhantomJS

# 打开网页
driver.get(url)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

3 等待策略
3.1 显式等待
显式等待使WebDriver等待某个条件成立时继续执行,否则在达到最长事件抛出超时异常(TimeoutException)

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get("https://www.baidu.com")

element = WebDriverWait(driver, 3, 0.5).until(
    EC.presence_of_element_located((By.ID, "kw"))
)
element.send_keys('python')
driver.quit()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

WebDriverWait类有WebDriver提供,在设置事件内,默认每间隔一段时间检测一次当前页面元素是否存在,如果超时则未检测到,则抛出异常。

WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)

driver:浏览器驱动
timeout:最长超时时间,默认单位为s
poll_frequency:超时后的异常信息,默认情况下抛–NoSuchElementException异常。
WebDriverWait()一般由until()或until_not()方法配合使用,下面是until()和until_not()方法的说明。

until(method, message=‘’) 调用该方法提供的驱动程序作为一个参数,直到返回值为True。
until_not(method, message=‘’) 调用该方法提供的驱动程序作为一个参数,直到返回值为False。
3.2 隐式等待
如果某些元素不是立即可用的,隐式等地啊是告诉WebDriver去等待一定时间后去查找元素。默认等待时间为0秒,一旦设置该值,隐式等待是设置该WebDriver的实例的生命周期。

from selenium import webdriver
from selenium.webdriver.common.by import By

# 加载驱动
driver = webdriver.Chrome()  # Chrome浏览器

url = "https://www.baidu.com/"
driver.set_window_size(1000, 900)

# 隐式等待
driver.implicitly_wait(10)  # seconds

driver.get(url)

driver.find_element(By.ID, "kw").clear()
driver.find_element(By.ID, "kw").send_keys("Python")
driver.find_element(By.ID, "su").click()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

4 浏览器控制
1、控制浏览器窗口大小

driver.set_window_size(900, 700)
  • 1

2、浏览器前进、后退

driver.forward()
driver.back()

  • 1
  • 2
  • 3

3、刷新

driver.refresh()

  • 1
  • 2

5 元素定位

# 通过id 查询首个满足条件
driver.find_element(by=By.ID, value="username")
# 通过class 查询所有满足条件 [列表形式返回]
driver.find_elements(by=By.CLASS_NAME, value="item")
# 通过TAG_NAME
driver.find_elements(by=By.TAG_NAME, value="a")
driver.find_elements(by=By.TAG_NAME, value="a").get_attribute("href")

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

参数一by :By.ID、By.CLASS_NAME、By.NAME
参数二value: 指定 ID、name或Class的值

4 具体使用
3.1 元素查找、定位
3.3 Webelement常用方法
1、点击和输入

driver.find_element(By.ID, "kw").clear()  				# 清除文本
driver.find_element(By.ID, "kw").send_keys("Python")    # 模拟按键输入
driver.find_element(By.ID, "su").click()  				# 点击元素

  • 1
  • 2
  • 3
  • 4

2、提交
模拟输入框回车操作

新版本中未测试出功能

  • 1
  • 2

3、其他

size:返回元素尺寸
text:获取元素的文本
get_attribute(name):获取属性值
is_displayed:设置元素是否用户可见

总结:

from selenium import webdriver

# 加载驱动
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()  # Chrome浏览器

# url = "https://read.douban.com/ebook/7821748/?dcs=search"
url = "https://www.baidu.com/"
driver.set_window_size(900, 700)
# 打开网页
driver.get(url)

driver.find_element(By.ID, "kw").clear()  # 清除文本
driver.find_element(By.ID, "kw").send_keys("Python")  # 模拟按键输入
driver.find_element(By.ID, "su").click()  # 点击元素

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

4 鼠标操作
selenium鼠标事件用的是ActionChains,在调用的时候并不会执行,而是在perform()方法被调用后执行。

引用 ActionCharins类

from selenium.webdriver.common.action_chains import ActionChains

  • 1
  • 2

4.1 单击/双击/鼠标右击

url = "https://www.baidu.com/"

  • 1
  • 2

实例:点击百度图片超链接

from selenium import webdriver
# 引用ActionChains类
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By

url = "https://www.baidu.com/"

driver = webdriver.Chrome()  # Chrome浏览器

# 隐式等待
driver.implicitly_wait(10)  # seconds

# 打开网页
driver.get(url)
rc = driver.find_element(by=By.LINK_TEXT, value="图片")
# 右击
# ActionChains(driver).context_click(rc).perform()
# 双击
# ActionChains(driver).double_click(rc).perform
# 单击
ActionChains(driver).click(on_element=rc).perform()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

4.2 移动元素
这个测试案例并不是很好,各位大佬如果有的话可以留言。

url = "https://www.baidu.com/"

  • 1
  • 2
# 移动元素
element = driver.find_element(By.LINK_TEXT, "图片")
target = driver.find_element(By.LINK_TEXT, "更多")
ActionChains(driver).drag_and_drop(element, target).perform()

  • 1
  • 2
  • 3
  • 4
  • 5

5 键盘事件
全选 Ctrol+A :send_keys(Keys.CONTROL, ‘a’)
复制 Ctrol+C:send_keys(Keys.CONTROL, ‘c’)
剪切 Ctrol+X:send_keys(Keys.CONTROL, ‘x’)
粘贴 Ctrol+V:send_keys(Keys.CONTROL, ‘v’)

键盘 F1:send_keys(Keys.F1)
…
键盘 F12:send_keys(Keys.F12)

删除键 BackSpace:send_keys(Keys.BACK_SPACE)

空格键 Space:send_keys(Keys.SPACE)

制表键 Tab:send_keys(Keys.TAB)

Esc键 Tab:send_keys(Keys.ESCAPE)

回车键 ENTER:send_keys(Keys.ENTER)

5.1 输入

from selenium import webdriver

# 引入 ActionChains 类
from selenium.webdriver import Keys

# 加载驱动
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()  # Chrome浏览器

url = "https://www.baidu.com/"
driver.set_window_size(1000, 900)

driver.get(url)

driver.find_element(By.ID, "kw").clear()
driver.find_element(By.ID, "kw").send_keys("Pythonn")
driver.find_element(By.ID, "kw").send_keys(Keys.BACK_SPACE)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

5.2 组合按键

# Ctrl+a
ActionChains(driver).key_down(Keys.CONTROL).send_keys("a").key_up(Keys.CONTROL).perform()

  • 1
  • 2
  • 3

6 获取页面信息

# 获取当前页面URL
now_url = driver.current_url
# 获取当前页面title
title = driver.title

  • 1
  • 2
  • 3
  • 4
  • 5

8 在不同窗口和框架之间移动

9 警告框处理

10 下拉选择框

11 文件上传

driver.find_element_by_name("file").send_keys('D:\\upload_file.txt')

  • 1
  • 2

12 cookie操作
WebDriver操作cookie的方法:

get_cookies(): 获得所有cookie信息。
get_cookie(name): 返回字典的key为“name”的cookie信息。
add_cookie(cookie_dict) : 添加cookie。“cookie_dict”指字典对象,必须有name 和value 值。
delete_cookie(name,optionsString):删除cookie信息。“name”是要删除的cookie的名称,“optionsString”是该cookie的选项,目前支持的选项包括“路径”,“域”。
delete_all_cookies(): 删除所有cookie信息

13 调用JavaScript代码

# 获取网页标题
driver.execute_script('return document.title;')

  • 1
  • 2
  • 3

14 关闭浏览器

# 关闭单个窗口
driver.close()

  • 1
  • 2
  • 3
# 关闭所有窗口
driver.quit()

  • 1
  • 2
  • 3
注:本文转载自blog.csdn.net的WUNEAL的文章"https://blog.csdn.net/WUNEAL/article/details/135213383"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

127
测试
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top