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

新手上路,请多包涵

我在使用 selenium 控制我的 Chrome 时遇到了麻烦。这是我的代码:

 from selenium import webdriver
driver = webdriver.Chrome()

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

 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

原文由 Weiziyoung 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 994
1 个回答

您需要提供 chromedriver 的路径…从 http://chromedriver.storage.googleapis.com/index.html?path=2.24/ 下载…解压缩并在…中提供路径… webdriver.chrome (“chromedriver 的路径”)

我在这里解释一下:

 from selenium import webdriver

driver = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")

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

     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

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

解决方案就像我提到的那样,您需要提供 chromedriver 的路径来代替 chrome 浏览器,例如

driver = webdriver.Chrome("E:\Jars\chromedriver.exe")

它将解决问题

原文由 thebadguy 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进