Selenium仅在无头模式下运行ChromeDriver

2024-10-16 20:48:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个在Firefox中使用Selenium的python脚本,但是在尝试运行同一个脚本时,chromedriver设置为drive Brave,这会产生错误:

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed
  (chrome not reachable)
  (The process started from chrome location /opt/brave.com/brave/brave is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

和调试日志:

DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:38723/session {"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"binary": "/opt/brave.com/brave/brave", "args": [], "extensions": []}, "browserName": "chrome", "platformName": "any"}, "firstMatch": [{}]}, "desiredCapabilities": {"goog:chromeOptions": {"binary": "/opt/brave.com/brave/brave", "args": [], "extensions": []}, "platform": "ANY", "browserName": "chrome", "version": ""}}
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): 127.0.0.1:38723
DEBUG:urllib3.connectionpool:http://127.0.0.1:38723 "POST /session HTTP/1.1" 500 318
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request

但是,如果我只添加了一个headless参数,脚本就会执行我希望它执行的操作(减去GUI):

from selenium import webdriver

driverPath = '/home/user/etc'
options = webdriver.ChromeOptions()
options.binary_location = '/opt/brave.com/brave/brave'
options.add_argument('--headless')
driver = webdriver.Chrome(executable_path=driverPath, options=options)

所以我想知道如何让勇敢的浏览器的GUI也运行。我的驱动程序和浏览器版本正确。我认为二进制路径是正确的


Tags: fromdebug脚本comremoteseleniumchromeconnection
1条回答
网友
1楼 · 发布于 2024-10-16 20:48:44

我记得Chrome headless是用Chrome59发货的。因此,在成功安装chrome之后,是否可以尝试从shell命令启动chrome并检查其安装是否正确

chrome \
   headless \                   # Runs Chrome in headless mode.
  https://stackoverflow.com      # URL to open.

chrome二进制名称可以是chrome、google chrome或其他明喻

相关问题 更多 >