0

我想通过铬运行硒。我写了这段代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument("--disable-gpu")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")
options.binary_location = "/snap/bin/chromium"
driver = webdriver.Chrome(chrome_options=options)

但是这段代码会抛出一个错误:

selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist
Stacktrace:
#0 0x55efd7355a23 <unknown>
#1 0x55efd6e20e18 <unknown>
#2 0x55efd6e46e12 <unknown>

正确版本的 chromodriver 在 usr/bin 中。我究竟做错了什么?

4

2 回答 2

0

我通过 apt 重新安装 chromium 解决了这个问题sudo apt install chromium-browser(在此之前它是通过 snap 安装的)。我的工作代码看起来像这样

options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")
if headless:
options.add_argument('--headless')
options.binary_location = "/usr/bin/chromium-browser"
self.driver = webdriver.Chrome(options=options)
于 2022-01-24T13:46:12.697 回答
0

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.

However you need to take careof a couple of things:

  • --disable-gpu was related to , so you need to drop it.

  • chrome_options is deprecated, use options instead.

    driver = webdriver.Chrome(options=options)
    

References

You can find a couple of relevant detailed discussions in:

于 2022-01-23T19:51:45.540 回答