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/')
相关推荐
se-tester7 天前
JMeter、Postman 和 SoapUI 在做接口测试上的优势和缺点
测试工具·jmeter·接口测试·postman·soapui
xixi09248 天前
selenium IDE——command
selenium·测试工具
xixi09248 天前
selenium IDE安装使用教程
ide·selenium·测试工具
Wpa.wk8 天前
har文件转为接口自动化测试用例
运维·测试工具·自动化·测试用例·接口自动化
Wpa.wk8 天前
ubuntu22桌面版安装
经验分享·测试工具
uyeonashi9 天前
自动化测试问卷考试系统测试报告
功能测试·selenium·github
007张三丰9 天前
软件测试专栏(6/20):Selenium从0到1实战指南:元素定位、等待机制与框架封装
selenium·测试工具
测试老哥9 天前
如何使用Postman做接口测试?
自动化测试·软件测试·python·测试工具·测试用例·接口测试·postman
安全不再安全9 天前
某驱动任意读漏洞分析 - 可用于游戏内存数据读取
c语言·测试工具·安全·游戏·网络安全