selenium报错:没有打开网页或selenium.common.exceptions.NoSuchDriverException

文章目录

问题

当selenium的环境配置没有问题,但在使用selenium访问浏览器时并没有打开网页,或者出现selenium.common.exceptions.NoSuchDriverException报错信息(如下图所示)。

以上问题可能的原因是没有配置chromedriver.exe的路径。(在虚拟环境中,有时候selenium会出现找不到chromedriver.exe路径的问题,这时候就需要手动配置其路径)

解决方法

从selenium中导入Service浏览器服务。

Service 是 Selenium WebDriver 中的一个类,它表示浏览器的服务。在使用 Selenium WebDriver 操作浏览器时,需要使用浏览器的驱动程序,而 Service 类提供了与其相关的管理和控制功能。

使用from selenium.webdriver.chrome.service import Service ,表示从 selenium 库中导入了 Service 类,用于管理 Chrome 浏览器的服务进程。一旦获取到 Service 的实例,就可以通过调用其方法来启动、停止和配置 Chrome 浏览器的服务。

注意 :使用 Service 类,需要提前下载并配置相应的驱动程序。例如,使用 Chrome 浏览器时需要下载 ChromeDriver,并将其路径相关联。

举例说明:

python 复制代码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

driver_path = r'D:\chromedriver-win32\chromedriver.exe'

service = Service(executable_path=driver_path)

driver = webdriver.Chrome(service=service)
driver.get('https://www.baidu.com/')
相关推荐
火云代理2 天前
同一个网站,有的网络能打开、有的打不开?认识 IPv4 与 IPv6 的访问差异
服务器·网络·网络协议·tcp/ip·测试工具·php·状态模式
freejackman7 天前
【工程化】从“单兵作战”到“多引擎架构”:基于门面模式的重构实战
大数据·数据库·hive·sql·测试工具·设计模式·架构
坐吃山猪7 天前
Python22-PostMan调试MCP
测试工具·postman·mcp
测试界的小码农8 天前
【 AI Agent 软件测试实战(05):任务规划与状态管理,让 Agent 学会拆解复杂测试任务】
人工智能·测试工具
和粒科技9 天前
热重分析仪TG 309 Libra Classic
测试工具
ClouGence10 天前
不搭 Selenium 或 Playwright,怎么测试公司的内网业务?
selenium·测试
螺丝钉tina15 天前
[Playwright] [02]常用命令与调试:Debug UI、Mode和 Trace Viewer
开发语言·javascript·测试工具·ui·自动化
梧桐16816 天前
14天、31个缺陷、28个测试脚本—用 Claude Code 完成第三方软件测试
人工智能·测试工具
mewowaBc17 天前
postman使用细则
测试工具·postman·偏差
光影62717 天前
Unittest单元测试基础速记
测试工具·单元测试·json·测试覆盖率