无法在无头模式下创建 Python Chrome WebDriver。
我使用的是 Chrome 版本 108.0.5359.94。
我还使用
Service(ChromeDriverManager().install())
自动安装兼容的 chromewebdriver。
注意:如果我下载适用的 chrome webdriver 并且不使用该服务,而是使用可执行文件路径,则会发生相同的结果。
重新创建此代码的代码非常简单。
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
chrome_options = Options()
# chrome_options.add_argument('--headless')
chrome_options.add_argument('--remote-debugging-port=9222')
chrome_options.add_argument('--no-sandbox')
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
print(driver)
以常规(非无头)模式运行时,我得到了 webdriver 对象的预期打印:
python selenium_test.py
<selenium.webdriver.chrome.webdriver.WebDriver (session="9b6ec32017d8d864f8e2061293faee40")>
但是当在无头模式下运行时(相同的代码只是从无头选项行中删除标记),我收到以下错误:
python selenium_test.py
Traceback (most recent call last):
File "/home/boazz/PycharmProjects/pythonProject/selenium_test.py", line 10, in <module>
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
File "/home/boazz/.virtualenvs/reigo_qa_39/lib/python3.9/site-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
super().__init__(DesiredCapabilities.CHROME['browserName'], "goog",
File "/home/boazz/.virtualenvs/reigo_qa_39/lib/python3.9/site-packages/selenium/webdriver/chromium/webdriver.py", line 92, in __init__
super().__init__(
File "/home/boazz/.virtualenvs/reigo_qa_39/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 270, in __init__
self.start_session(capabilities, browser_profile)
File "/home/boazz/.virtualenvs/reigo_qa_39/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 363, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/boazz/.virtualenvs/reigo_qa_39/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 428, in execute
self.error_handler.check_response(response)
File "/home/boazz/.virtualenvs/reigo_qa_39/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 243, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed.
(chrome not reachable)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Stacktrace:
#0 0x5588159a82a3 <unknown>
#1 0x558815766f77 <unknown>
#2 0x55881578f5f7 <unknown>
#3 0x55881578b7d0 <unknown>
#4 0x5588157cc0b7 <unknown>
#5 0x5588157cba5f <unknown>
#6 0x5588157c3903 <unknown>
#7 0x558815796ece <unknown>
#8 0x558815797fde <unknown>
#9 0x5588159f863e <unknown>
#10 0x5588159fbb79 <unknown>
#11 0x5588159de89e <unknown>
#12 0x5588159fca83 <unknown>
#13 0x5588159d1505 <unknown>
#14 0x558815a1dca8 <unknown>
#15 0x558815a1de36 <unknown>
#16 0x558815a39333 <unknown>
#17 0x7fa6443c0b43 <unknown>
我希望无论我是否使用无头选项,都会创建 chrome 驱动程序。可能是什么原因造成的?
此处的问题在于您尝试设置用于调试的端口。
我尝试了你的代码,它对我也不起作用,但是当我将端口从
9222
更改为 61625
时,它起作用了。from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
options.add_argument('--remote-debugging-port=61625')
options.add_argument('--no-sandbox')
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
print(driver)
输出是
<selenium.webdriver.chrome.webdriver.WebDriver (session="70ca99a865d0eb7b93a95f530ef1401b")>
您尝试过不同的方法吗? 就像使用 WebDriverManager 创建驱动程序一样,请参阅下面的链接: https://pypi.org/project/webdriver-manager