首页 > 解决方案 > Selenium.webdriver:Chrome 无法启动:崩溃。作为 Cronjob 运行时

问题描述

我正在尝试让我的 selenium 脚本作为 Cron 运行。该脚本在终端上运行没有问题,并为我提供了所需的输出。但是作为 Cron 运行我得到了错误:

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 /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

这是我的驱动程序初始化。我在代码中添加了应该解决问题的所有内容,但它对我不起作用:

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument("--incognito")
chrome_options.add_argument("--disable-plugins-discovery")
chrome_options.add_argument("--start-maximized")
chrome_options.binary_location = "/usr/bin/google-chrome"
chrome_options.headless = True
caps = DesiredCapabilities().CHROME
caps["marionette"] = True
os.environ["webdriver.chrome.driver"] = '/usr/bin/chromedriver'
driver = webdriver.Chrome(executable_path='/usr/bin/chromedriver',options=chrome_options) # <-here it crashes
driver.delete_all_cookies()
driver.set_window_size(1920, 1080)
driver.set_window_position(0, 0)

chromedriver --version 和 whereis chromedriver:

ChromeDriver 86.0.4240.22 (398b0743353ff36fb1b82468f63a3a93b4e2e89e-refs/branch-heads/4240@{#378})
chromedriver: /usr/bin/chromedriver /usr/local/bin/chromedriver

google-chrome --version 和 whereis google-chrome:

Google Chrome 86.0.4240.183
google-chrome: /usr/bin/google-chrome

就像我之前说的那样,它在终端上运行,但是通过这个 cronjob,我得到了上面的错误:

*/5 * * * * user ~/HTGMQ/run.sh

运行.sh

#!/bin/bash
cd ~/HTGMQ/ # i know its redundant but who cares
/usr/bin/python3 ~/HTGMQ/script.py

我真的不知道我该怎么办了:(

谢谢回复和回答

标签: python-3.xseleniumselenium-webdrivercronselenium-chromedriver

解决方案


推荐阅读