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面试】第一时间阅读,回复【资料】获取福利,回复【项目】获取项目源码,回复【简历模板】获取简历模板,回复【学习路线图】获取学习路线图。

相关推荐
程序员三藏4 小时前
Jmeter接口测试与压力测试
自动化测试·软件测试·python·测试工具·jmeter·接口测试·压力测试
一晌小贪欢6 小时前
Python爬虫第6课:Selenium自动化浏览器与动态内容抓取
爬虫·python·selenium·网络爬虫·python基础·python3·pathon爬虫
桃子不淘气6 小时前
2:测试平台之DB构建
测试工具
测试老哥8 小时前
Postman环境变量设置全攻略
自动化测试·软件测试·python·测试工具·职场和发展·接口测试·postman
胜天半月子9 小时前
接口测试 | 使用Postman实际场景化测试
测试工具·接口测试·postman
〆WangBenYan゜10 小时前
postman 调用接口设置全局变量
测试工具·lua·postman
程序员小远16 小时前
软件测试之压力测试详解
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·压力测试
大面积秃头17 小时前
postman工具使用前置知识
测试工具·postman
程序员大辉17 小时前
接口调试利器,Postman免安装,免登陆
测试工具·lua·postman