【问题标题】:selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed with ChromeDriver and Selenium in Pythonselenium.common.exceptions.WebDriverException:消息:未知错误:Chrome 无法启动:在 Python 中使用 ChromeDriver 和 Selenium 崩溃
【发布时间】:2018-08-31 09:13:39
【问题描述】:

这是我的代码脚本:

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:\Python\chromedriver.exe", chrome_options=options)
w.get("https://www.facebook.com")

在运行此脚本时出现此错误:

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:\Users\hadi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 75, in __init__
    desired_capabilities=desired_capabilities)
  File "C:\Users\hadi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 154, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "C:\Users\hadi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 243, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Users\hadi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "C:\Users\hadi\AppData\Local\Programs\Python\Python36-32\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
  (Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 10.0.17134 x86_64)

我已经编辑了 chromedriver 的可执行路径,但是当我运行脚本时,我的 chrome 驱动程序会打开,但之后会卡住 2-3 分钟,然后因上述以下错误而崩溃。

【问题讨论】:

  • 我认为你应该在你的 executable_path 中去掉反斜杠
  • 抱歉问题已编辑

标签: python selenium google-chrome selenium-chromedriver chrome-profile


【解决方案1】:

拇指规则

Chrome 在启动期间崩溃的一个常见原因是在 Linux 上以 root 用户 (administrator) 运行 Chrome。虽然可以通过在创建 WebDriver 会话时传递 --no-sandbox 标志来解决此问题,但不支持并且强烈建议不要使用此类配置。您需要将环境配置为以普通用户身份运行 Chrome。


根据您的代码试验,您似乎正在尝试访问 Chrome 配置文件,因此您可以使用以下解决方案:

  • 代码块:

    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\to\chromedriver.exe', chrome_options=options)
    driver.get("https://www.google.co.in")
    
  • 在这里你可以找到How to open a Chrome Profile through Python的详细讨论

【讨论】:

  • 先生,我不能在 chrome webdriver 中使用我的默认配置文件吗?
  • @GigaByte 您的 默认 chrome 配置文件 将包含许多 书签扩展主题 i>、cookies 等。Selenium 可能无法加载它。因此,按照最佳实践,为您的@Test 创建一个新的 chrome 配置文件,并在 配置文件 中存储/保存/配置所需的数据。
  • 意味着当我为我创建@test 配置文件时,之后我可以使用该活动配置文件的缓存吗?是指我将进行的搜索?
  • stackoverflow.com/questions/49270109/… 这个链接对创建和使用 webdriver 配置文件有帮助吗?
  • 得到结果。先生,您救了我的命。我最近两天一直在尝试,现在因为你而得到了结果。您总是在我的帖子上提供帮助,谢谢 alotttttttt。
猜你喜欢
  • 1970-01-01
  • 2020-11-01
  • 2019-02-27
  • 2020-04-16
  • 2020-05-21
  • 1970-01-01
  • 2018-02-07
  • 1970-01-01
  • 2020-08-19
相关资源
最近更新 更多