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/')
相关推荐
DeamoTech3 小时前
ESCADA
物联网·测试工具
旷世奇才李先生14 小时前
Selenium 安装使用教程
selenium·测试工具
巴里巴气1 天前
对selenium进行浏览器和驱动进行配置Windows | Linux
selenium·测试工具
q567315232 天前
Java Selenium反爬虫技术方案
java·爬虫·selenium
有趣的我2 天前
wireshark介绍和使用
网络·测试工具·wireshark
草履虫建模2 天前
Postman - API 调试与开发工具 - 标准使用流程
java·测试工具·spring·json·测试用例·postman·集成学习
龙潜月七2 天前
Selenium 自动化测试中跳过机器人验证的完整指南:能用
python·selenium·机器人
AIZHINAN2 天前
如何评价 selenium 自动化测试框架搭建?
selenium·测试工具
WIN赢11 天前
PostMan使用
测试工具·lua·postman
百里图书13 天前
颠覆传统接口测试!用 Streamlit + SQLite + GPT 打造可视化自动化平台
自动化测试·测试开发·接口自动化测试·测试工具·接口测试·测试平台·python编程