Python Selenium Webdriver不在headless模式下工作



无法在headless模式下创建Python Chrome WebDriver。

我使用的Chrome版本是108.0.5359.94。

我也使用Service(ChromeDriverManager().install())来自动安装兼容的chromewebdriver。

注意:同样的结果发生,如果我下载适用的chrome webdriver,而不是使用服务,我使用executable_path.

重新创建的代码非常简单。

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:webdriverschromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
print(driver)

输出为

<selenium.webdriver.chrome.webdriver.WebDriver (session="70ca99a865d0eb7b93a95f530ef1401b")>

相关内容

  • 没有找到相关文章

最新更新