我在使用 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 许可协议
您需要提供 chromedriver 的路径…从 http://chromedriver.storage.googleapis.com/index.html?path=2.24/ 下载…解压缩并在…中提供路径… webdriver.chrome (“chromedriver 的路径”)
我在这里解释一下:
如果我运行上面的代码,这是错误:
这与@Weiziyoung 在原始问题中提到的相同。
解决方案就像我提到的那样,您需要提供 chromedriver 的路径来代替 chrome 浏览器,例如
它将解决问题