当前位置: 首页 > 面试题库 >

selenium.common.exceptions.WebDriverException:消息:未知错误:Chrome无法启动:使用Python中的ChromeDriver和Selenium崩溃

陶裕
2023-03-14
问题内容

这是我的代码脚本:

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分钟,然后由于上述错误而崩溃。


问题答案:

拇指法则

Chrome在启动过程中崩溃的常见原因是 在Linux上以 root 用户( administrator
)的身份运行Chrome 。虽然可以--no- sandbox在创建WebDriver会话时通过传递标志来解决此问题,但这种配置不受支持,因此不建议使用。您需要配置您的环境以改为以普通用户身份运行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")



 类似资料: