selenium.common.exceptions.WebDriverException:消息:未知错误:Chrome 无法启动:在 Python 中使用 ChromeDriver 和 Selenium 崩溃
问题描述
这是我的代码脚本:
从 selenium 导入 webdriver选项 = webdriver.ChromeOptions()options.add_argument("user-data-dir=C:\Users\hadi\AppData\Local\Google\Chrome\User Data") #您的 chrome 配置文件的路径w = webdriver.Chrome(executable_path="E:Pythonchromedriver.exe", chrome_options=options)w.get("https://www.facebook.com")
在运行此脚本时出现此错误:
Traceback(最近一次调用最后一次):<module> 中的文件E:/Python/MoosaBhai/TestoTes.py",第 6 行w = webdriver.Chrome(executable_path="E:\Python\chromedriver.exe", chrome_options=options)__init__ 中的文件C:UsershadiAppDataLocalProgramsPythonPython36-32libsite-packagesseleniumwebdriverchromewebdriver.py",第 75 行期望的能力=期望的能力)文件C:UsershadiAppDataLocalProgramsPythonPython36-32libsite-packagesseleniumwebdriveremotewebdriver.py",第 154 行,在 __init__self.start_session(desired_capabilities, browser_profile)文件C:UsershadiAppDataLocalProgramsPythonPython36-32libsite-packagesseleniumwebdriveremotewebdriver.py",第 243 行,在 start_session响应 = self.execute(Command.NEW_SESSION,参数)文件C:UsershadiAppDataLocalProgramsPythonPython36-32libsite-packagesseleniumwebdriveremotewebdriver.py",第 312 行,在执行中self.error_handler.check_response(响应)文件C:UsershadiAppDataLocalProgramsPythonPython36-32libsite-packagesseleniumwebdriveremoteerrorhandler.py",第 242 行,在 check_response引发异常类(消息、屏幕、堆栈跟踪)selenium.common.exceptions.WebDriverException:消息:未知错误:Chrome 无法启动:崩溃(驱动程序信息:chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),平台=Windows NT 10.0.17134 x86_64)
我已经编辑了 chromedriver 的可执行路径,但是当我运行脚本时,我的 chrome 驱动程序会打开,但之后会卡住 2-3 分钟,然后因上述以下错误而崩溃.
解决方案拇指规则
<块引用>Chrome 在启动过程中崩溃的一个常见原因是作为 root
用户(administrator
运行 Chrome>) 在 Linux 上.虽然可以通过在创建 WebDriver 会话时传递 --no-sandbox
标志来解决此问题,但这种配置不受支持且不鼓励使用.您需要将环境配置为以普通用户身份运行 Chrome.
根据您的代码试验,您似乎正在尝试访问 Chrome 配置文件,因此您可以使用以下解决方案:
代码块:
从 selenium 导入 webdriver从 selenium.webdriver.chrome.options 导入选项选项=选项()options.add_argument("user-data-dir=C:\path\to\your\profile\Google\Chrome\User Data\Profile 2")driver = webdriver.Chrome(executable_path=r'C:path ochromedriver.exe', chrome_options=options)driver.get("https://www.google.co.in")
您可以在此处找到详细讨论 如何通过 Python 打开 Chrome 配置文件
This is my code script:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\Users\hadi\AppData\Local\Google\Chrome\User Data") #Path to your chrome profile
w = webdriver.Chrome(executable_path="E:Pythonchromedriver.exe", chrome_options=options)
w.get("https://www.facebook.com")
and on running this script i'm getting this error:
Traceback (most recent call last):
File "E:/Python/MoosaBhai/TestoTes.py", line 6, in <module>
w = webdriver.Chrome(executable_path="E:\Python\chromedriver.exe", chrome_options=options)
File "C:UsershadiAppDataLocalProgramsPythonPython36-32libsite-packagesseleniumwebdriverchromewebdriver.py", line 75, in __init__
desired_capabilities=desired_capabilities)
File "C:UsershadiAppDataLocalProgramsPythonPython36-32libsite-packagesseleniumwebdriveremotewebdriver.py", line 154, in __init__
self.start_session(desired_capabilities, browser_profile)
File "C:UsershadiAppDataLocalProgramsPythonPython36-32libsite-packagesseleniumwebdriveremotewebdriver.py", line 243, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:UsershadiAppDataLocalProgramsPythonPython36-32libsite-packagesseleniumwebdriveremotewebdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "C:UsershadiAppDataLocalProgramsPythonPython36-32libsite-packagesseleniumwebdriveremoteerrorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 10.0.17134 x86_64)
I've edited my executeable path to chromedriver but when i run the script my chrome driver opens but after that stuck for 2-3minutes and then crashes with the above following error.
解决方案Thumb rule
A common cause for Chrome to crash during startup is running Chrome as
root
user (administrator
) on Linux. While it is possible to work around this issue by passing--no-sandbox
flag when creating your WebDriver session, such a configuration is unsupported and highly discouraged. You need to configure your environment to run Chrome as a regular user instead.
As per your code trials seems you are trying to access a Chrome Profile so you can use the following solution:
Code Block:
from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.add_argument("user-data-dir=C:\path\to\your\profile\Google\Chrome\User Data\Profile 2") driver = webdriver.Chrome(executable_path=r'C:path ochromedriver.exe', chrome_options=options) driver.get("https://www.google.co.in")
Here you can find a detailed discussion in How to open a Chrome Profile through Python
相关文章