Selenium 安装使用教程

一、Selenium 简介

Selenium 是一个用于 Web 应用程序自动化测试的工具,它支持多种浏览器(如 Chrome、Firefox、Edge 等)以及多种编程语言接口(如 Python、Java、C# 等)。在 Python 中,Selenium 常用于网页爬虫、自动化测试、自动化表单填写等场景。


二、安装 Selenium

2.1 安装 Selenium 包

bash 复制代码
pip install selenium

2.2 安装浏览器驱动

Selenium 依赖浏览器驱动与浏览器进行通信,需下载与你的浏览器版本匹配的驱动:

Chrome(chromedriver)
  1. 查看 Chrome 浏览器版本;
  2. 下载对应版本的 chromedriver:https://chromedriver.chromium.org/downloads;
  3. 解压后将其路径添加到系统环境变量中,或在代码中指定路径。
Edge(msedgedriver)

下载地址:https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/

Firefox(geckodriver)

下载地址:https://github.com/mozilla/geckodriver/releases


三、第一个 Selenium 示例(以 Chrome 为例)

python 复制代码
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

# 启动浏览器
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

# 打开网页
driver.get("https://www.python.org")

# 查找元素
search_box = driver.find_element(By.NAME, "q")
search_box.send_keys("selenium")
search_box.submit()

# 等待页面加载
driver.implicitly_wait(5)

# 输出页面标题
print(driver.title)

# 关闭浏览器
driver.quit()

四、常见操作

4.1 查找元素

python 复制代码
driver.find_element(By.ID, "id")
driver.find_element(By.NAME, "name")
driver.find_element(By.XPATH, "//div[@class='class']")

4.2 页面交互

python 复制代码
element.click()
element.send_keys("text")
element.clear()

4.3 窗口控制

python 复制代码
driver.maximize_window()
driver.back()
driver.refresh()

4.4 等待操作

python 复制代码
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "some_id")))

五、使用 headless(无头)模式

python 复制代码
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("--headless")
driver = webdriver.Chrome(options=options)

六、使用 WebDriver Manager(自动管理驱动)

bash 复制代码
pip install webdriver-manager

示例(Chrome):

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()))

七、常见问题

Q1: 报错"WebDriverException: Message: 'chromedriver' executable needs to be in PATH"?

需确保浏览器驱动位于环境变量中,或使用 webdriver-manager 自动管理驱动。

Q2: selenium.common.exceptions.NoSuchElementException?

检查元素是否真的存在,可使用 WebDriverWait 等待元素加载。


八、学习资源推荐


本文由"小奇Java面试"原创发布,转载请注明出处。

可以搜索【小奇JAVA面试】第一时间阅读,回复【资料】获取福利,回复【项目】获取项目源码,回复【简历模板】获取简历模板,回复【学习路线图】获取学习路线图。

相关推荐
流㶡3 小时前
Python爬虫:POST与Selenium
爬虫·python·selenium
写点什么呢4 小时前
Ltspice_安装与使用
学习·测试工具
介一安全7 小时前
OSS-Fuzz 模糊测试使用指南
测试工具·安全性测试·fuzz
0思必得017 小时前
[Web自动化] Selenium处理动态网页
前端·爬虫·python·selenium·自动化
llilian_161 天前
信号发生器 多通道多功能脉冲信号发生器应用解决方案 多功能脉冲发生器
功能测试·单片机·嵌入式硬件·测试工具
程序员小远1 天前
使用Postman进行一次完整的接口测试
自动化测试·软件测试·python·测试工具·测试用例·接口测试·postman
我的xiaodoujiao1 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 44--将自动化测试结果自动推送至钉钉工作群聊
前端·python·测试工具·ui·pytest
生活很暖很治愈1 天前
GUI自动化测试[3]——控件&数鼠标操作
windows·python·功能测试·测试工具
我的xiaodoujiao1 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 45--生成项目需要的requirements.txt依赖文件
python·学习·测试工具·pytest
uyeonashi1 天前
Boost搜索引擎测试报告
功能测试·selenium·搜索引擎