selenium.common.exceptions.WebDriverException:消息:服务

Wei*_*ung 3 selenium google-chrome web-crawler python-3.x selenium-webdriver

当我使用硒来控制Chrome时,我遇到了麻烦.这是我的代码:

from selenium import webdriver
driver = webdriver.Chrome()
Run Code Online (Sandbox Code Playgroud)

当我尝试操作它时,它首先成功运行,Chrome在屏幕上弹出.然而,它在几秒钟后关闭. 这是Traceback信息

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    driver = webdriver.Chrome('C:\Program Files (x86)\Google\Chrome\chrome.exe')
  File "C:\Users\35273\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 62, in __init__
    self.service.start()
  File "C:\Users\35273\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 86, in start
    self.assert_process_still_running()
  File "C:\Users\35273\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 99, in assert_process_still_running
    % (self.path, return_code)
selenium.common.exceptions.WebDriverException: Message: Service C:\Program Files (x86)\Google\Chrome\chrome.exe unexpectedly exited. Status code was: 0
Run Code Online (Sandbox Code Playgroud)

the*_*guy 6

您需要提供chromedriver的路径...从http://chromedriver.storage.googleapis.com/index.html?path=2.24 /...unzip下载并在... webdriver.chrome中提供它的路径("通往chromedriver的路径")

我在这里解释一下:

from selenium import webdriver


driver = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")
Run Code Online (Sandbox Code Playgroud)

如果我运行上面的代码,这是错误:

    C:\Python27\python.exe C:/Users/Gaurav.Gaurav-PC/PycharmProjects/Learning/StackOverflow/SeleniumQuestion/test123.py
    Traceback (most recent call last):
      File "C:/Users/Gaurav.Gaurav-PC/PycharmProjects/Learning/StackOverflow/SeleniumQuestion/test123.py", line 4, in <module>
        driver = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")
      File "C:\Python27\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 62, in __init__
        self.service.start()
      File "C:\Python27\lib\site-packages\selenium\webdriver\common\service.py", line 86, in start
        self.assert_process_still_running()
      File "C:\Python27\lib\site-packages\selenium\webdriver\common\service.py", line 99, in assert_process_still_running
        % (self.path, return_code)
    selenium.common.exceptions.WebDriverException: Message: Service C:\Program Files (x86)\Google

\Chrome\Application\chrome.exe unexpectedly exited. Status code was: 0
Run Code Online (Sandbox Code Playgroud)

这与@Weiziyoung在原始问题中提到的相同.

正如我所提到的,解决方案需要提供chromedriver的路径来代替chrome浏览器

driver = webdriver.Chrome("E:\Jars\chromedriver.exe")
Run Code Online (Sandbox Code Playgroud)

它将解决问题

  • OP不需要指定文件的直接路径,因为似乎chromedriver已经在Path中。如果未找到“ chromedriver”二进制文件,则类似“ selenium.common.exceptions.WebDriverException”的消息:消息:“ chromedriver”可执行文件需要放在PATH中。 (2认同)