English 中文(简体)
Python 网上用户不以无情的方式工作
原标题:Python Selenium Webdriver is not working in headless mode

无法以无动于衷的方式制造 Python网络司机。

我正在使用108.0.5359.94号 Chrome版。

我也正在使用<条码>服务(Chrome/63/7rManager().install(),以自动安装兼容的hr网。

NOTE: The same result happens if I download the applicable chrome webdriver and instead of using the service I use the 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)

在采用正常(并非无主)模式时,我正在得到网上用户物体的预期印刷:

python selenium_test.py 
<selenium.webdriver.chrome.webdriver.WebDriver (session="9b6ec32017d8d864f8e2061293faee40")>

But when running in headless mode (same code simply remove the marker from the headless option line), I am getting the following error:

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>

我预计,无论我是否使用无头选择,都不会产生神职司机。 原因何在?

问题回答

The issue here is with a port you trying to set for debugging.
I tried your code and it did not work for me too, but when I changed the port from 9222 to 61625 it worked.
So, at least for me this worked:

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")>

Have you tried a different approach? Like using WebDriverManager to create your driver, see link below: https://pypi.org/project/webdriver-manager





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签