带你认识Selenium函数

Selenium除了用于Web应用程序的测试外,还可以执行许多自动化操作。以下是一些Selenium可以实现的功能,并附带相应的代码示例来详细说明:

  1. 自动化操作

使用Selenium,我们可以模拟用户的行为,如点击、输入、滚动等。

复制代码

python复制代码

|---|----------------------------------------------------------|
| | from selenium import webdriver |
| | from selenium.webdriver.common.keys import Keys |
| | from selenium.webdriver.common.by import By |
| | |
| | # 初始化浏览器驱动 |
| | driver = webdriver.Chrome() |
| | |
| | # 打开网页 |
| | driver.get('https://www.example.com') |
| | |
| | # 查找元素并点击 |
| | search_box = driver.find_element(By.ID, 'search-box') |
| | search_box.send_keys('Selenium') |
| | search_box.send_keys(Keys.RETURN) |
| | |
| | # 关闭浏览器 |
| | driver.quit() |

  1. 跨浏览器测试

Selenium支持多种浏览器,只需更换对应的WebDriver即可。

复制代码

python复制代码

|---|---------------------------------|
| | # 对于Firefox浏览器 |
| | driver = webdriver.Firefox() |
| | |
| | # 对于Chrome浏览器 |
| | driver = webdriver.Chrome() |
| | |
| | # 对于Edge浏览器 |
| | driver = webdriver.Edge() |
| | |
| | # ... 以此类推,根据需要选择浏览器 |

  1. 截图功能

Selenium允许我们捕获当前页面的截图。

复制代码

python复制代码

|---|--------------------------------------------------------------------------------|
| | from selenium import webdriver |
| | from selenium.webdriver.chrome.service import Service |
| | from webdriver_manager.chrome import ChromeDriverManager |
| | |
| | # 初始化浏览器驱动 |
| | driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())) |
| | |
| | # 打开网页 |
| | driver.get('https://www.example.com') |
| | |
| | # 截图 |
| | driver.save_screenshot('screenshot.png') |
| | |
| | # 关闭浏览器 |
| | driver.quit() |

  1. 执行JavaScript脚本

Selenium提供了执行JavaScript代码的方法。

复制代码

python复制代码

|---|-------------------------------------------------------|
| | from selenium import webdriver |
| | |
| | driver = webdriver.Chrome() |
| | driver.get('https://www.example.com') |
| | |
| | # 执行JavaScript脚本 |
| | driver.execute_script("alert('Hello, Selenium!')") |
| | |
| | # 关闭浏览器 |
| | driver.quit() |

  1. 表单验证测试

Selenium可以模拟表单提交,并检查验证结果。

复制代码

python复制代码

|---|------------------------------------------------------------------|
| | from selenium import webdriver |
| | from selenium.webdriver.common.by import By |
| | from selenium.common.exceptions import NoSuchElementException |
| | |
| | driver = webdriver.Chrome() |
| | driver.get('https://www.example.com/login') |
| | |
| | # 输入用户名和密码 |
| | username_input = driver.find_element(By.ID, 'username') |
| | username_input.send_keys('my_username') |
| | |
| | password_input = driver.find_element(By.ID, 'password') |
| | password_input.send_keys('my_password') |
| | |
| | # 提交表单 |
| | submit_button = driver.find_element(By.ID, 'submit') |
| | submit_button.click() |
| | |
| | try: |
| | # 检查是否出现错误消息 |
| | error_message = driver.find_element(By.ID, 'error-message') |
| | print("Error:", error_message.text) |
| | except NoSuchElementException: |
| | print("Form submitted successfully!") |
| | |
| | # 关闭浏览器 |
| | driver.quit() |

  1. 等待AJAX请求完成

Selenium提供了显式等待(Explicit Waits)来处理AJAX请求。

复制代码

python复制代码

|---|----------------------------------------------------------------------------------|
| | 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.Chrome() |
| | driver.get('https://www.example.com/ajax-page') |
| | |
| | # 等待某个元素出现 |
| | wait = WebDriverWait(driver, 10) |
| | element = wait.until(EC.visibility_of_element_located((By.ID, 'my-element'))) |
| | |
| | # 执行后续操作 |
| | print(element.text) |
| | |
| | # 关闭浏览器 |
| | driver.quit() |

  1. 与其他测试框架集成

Selenium可以很容易地与unittest、pytest等测试框架集成,实现测试用例的编写和运行。

以pytest为例,你可以编写如下的测试用例:

复制代码

python复制代码

|---|------------------------------------------------|
| | import pytest |
| | from selenium import webdriver |
| | from selenium.webdriver.common.by import By |
| | |
| | @pytest.fixture(scope="module") |
| | def driver(request): |
| | wd = webdriver.Chrome() |
| | wd.implicitly_wait(10) |
| | request.addfinalizer(wd.quit) |
| | return wd |
| | |
| | def test_example_com(driver): |
| | driver.get("https://www.example.com") |
| | assert "Example Domain" |

相关推荐
冷雨夜中漫步3 小时前
Python快速入门(6)——for/if/while语句
开发语言·经验分享·笔记·python
郝学胜-神的一滴3 小时前
深入解析Python字典的继承关系:从abc模块看设计之美
网络·数据结构·python·程序人生
百锦再3 小时前
Reactive编程入门:Project Reactor 深度指南
前端·javascript·python·react.js·django·前端框架·reactjs
喵手5 小时前
Python爬虫实战:旅游数据采集实战 - 携程&去哪儿酒店机票价格监控完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集结果csv导出·旅游数据采集·携程/去哪儿酒店机票价格监控
2501_944934735 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
helloworldandy5 小时前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
肖永威6 小时前
macOS环境安装/卸载python实践笔记
笔记·python·macos
TechWJ7 小时前
PyPTO编程范式深度解读:让NPU开发像写Python一样简单
开发语言·python·cann·pypto
枷锁—sha7 小时前
【SRC】SQL注入WAF 绕过应对策略(二)
网络·数据库·python·sql·安全·网络安全
abluckyboy7 小时前
Java 实现求 n 的 n^n 次方的最后一位数字
java·python·算法