简体   繁体   中英

Headless Selenium on Ubuntu Server

I currently have selenium installed on a ubuntu 20.04.2 server and I am unable to get it to work, I am not sure what I am missing here

This is the current python code, I've tried it with and without each one of the options below

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

firefox_options = webdriver.FirefoxOptions()
firefox_options.add_argument("-headless")
firefox_options.add_argument("--screenshot")
firefox_options.add_argument("--no-sandbox")
firefox_options.add_argument("--disable-dev-shm-usage")
firefox_options.add_argument("-disable-gpu")

browser = webdriver.Firefox(executable_path="./drivers/geckodriver", options=firefox_options)

browser.get('http://www.google.com')

This is the python error code:

simple.py:11: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  browser = webdriver.Firefox(executable_path="./drivers/geckodriver", options=firefox_options)
Traceback (most recent call last):
  File "simple.py", line 11, in <module>
    browser = webdriver.Firefox(executable_path="./drivers/geckodriver", options=firefox_options)
  File "/usr/lib/python3/dist-packages/selenium/webdriver/firefox/webdriver.py", line 170, in __init__
    RemoteWebDriver.__init__(
  File "/usr/lib/python3/dist-packages/selenium/webdriver/remote/webdriver.py", line 152, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/lib/python3/dist-packages/selenium/webdriver/remote/webdriver.py", line 249, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/lib/python3/dist-packages/selenium/webdriver/remote/webdriver.py", line 318, in execute
    self.error_handler.check_response(response)
  File "/usr/lib/python3/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: Connection refused (os error 111)

And this is the geckodriver log:

1614163729970   geckodriver     INFO    Listening on 127.0.0.1:50115
1614163730921   mozrunner::runner       INFO    Running command: "/snap/bin/firefox" "--marionette" "-headless" "-->
*** You are running in headless mode.
[GFX1-]: glxtest: libpci missing
[GFX1-]: glxtest: Unable to open a connection to the X server
[GFX1-]: glxtest: libEGL initialize failed
[GFX1-]: No GPUs detected via PCI

The syslog has nothing related to firefox

If anyone else comes across this issue, re-installing Firefox solved the issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM