当前位置:   article > 正文

selenium报错:没有打开网页或selenium.common.exceptions.NoSuchDriverException_python selenium没有访问

python selenium没有访问

文章目录

问题

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,并将其路径相关联。

举例说明:

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/')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/240188
推荐阅读
相关标签
  

闽ICP备14008679号