【发布时间】:2019-06-18 02:01:15
【问题描述】:
我正在尝试通过 Selenium 打开 Chrome 网络驱动程序,但在初始化时出现错误。我收到的错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "F:\WinPython\python-3.6.7.amd64\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "F:\WinPython\python-3.6.7.amd64\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "F:\WinPython\python-3.6.7.amd64\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "F:\WinPython\python-3.6.7.amd64\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "F:\WinPython\python-3.6.7.amd64\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location C:\Users\MyName\AppData\Local\Microsoft\AppV\Client\Integration\some-ever-changing-hash\Root\VFS\ProgramFilesX86\Google\Chrome\Application\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
(Driver info: chromedriver=2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387),platform=Windows NT 10.0.14393 x86_64)
我一直在寻找不同的解决方案。我尝试手动指定 Chrome.exe 的路径,我添加了 sn-ps,如 add_argument("--disable-dev-shm-usage") 和 options.add_argument("--no-sandbox") 以及使用单独的用户数据目录。当前代码如下所示
import os
from selenium import webdriver
###auto-find chrome path
def chrome_path_auto():
for root, dirs, files in os.walk('C:/Users'):
for name in files:
if name == 'chrome.exe':
return os.path.abspath(os.path.join(root, name))
options = webdriver.ChromeOptions()
chrome_driver_binary = "H:/My Documents/PYTHON/selenium_script/chromedriver/chromedriver.exe"
options.binary_location = chrome_path_auto()
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--user-data-dir=H:\\My Documents\\PYTHON\\selenium_script\\UserDataDir")
options.add_argument("--disable-extensions")
options.add_argument("--no-sandbox")
driver = webdriver.Chrome(chrome_driver_binary, chrome_options=options)
driver.get('https://python.org')
Chrome的版本是71.0.3578.80,Chromedriver是2.45,应该是兼容的。我试过使用旧版本,但我只收到部分错误:DevToolsActivePort 文件不存在。 可能需要注意的是,我使用的远程桌面最近将操作系统从 Windows 7 更改为 Windows 10。在旧 Windows 上,一种非常相似的方法(使用 Chrome 和 Chromedriver 的旧版本)正在工作。有没有人遇到过类似的问题或可以想到替代解决方案?
如果还有什么需要请告诉我,我会尽量提供。
编辑 1.
我通过简单地将整个 Chrome 安装从原始文件夹
C:\Users\MyName\AppData\Local\Microsoft\AppV\Client\Integration\some-ever-changing-hash\Root\VFS\ProgramFilesX86\Google\Chrome\Application\chrome.exe 移动到共享磁盘 H:\Public\Chrome\Application 然后使用 binary_location 来解决这个问题。它可能与原始文件夹的根位置和缺乏管理员权限有关。
【问题讨论】:
标签: python selenium selenium-chromedriver