I'm having troubles setting up Geckodriver to run with Selenium on a Ubuntu VPS, referring to this previously asked question.
My current setup:
- Selenium Version: 4.17.2
- Firefox Version: 122.0
- Geckodriver Version: 0.34.0
- Python Version: 3.8.10
- Ubuntu Version: 20.04.5 LTS
Steps:
- Installing all the necessary packages
sudo apt-get update
sudo apt-get install firefox
pip install selenium
tar -xvzf geckodriver-v0.34.0-linux64.tar.gz
chmod +x geckodriver
sudo mv geckodriver /usr/local/bin/
The last command puts geckodriver on PATH, this is needed according to this post
- Verifying installation
firefox --version
./geckodriver --version
pip show selenium
- Executing a test script
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
driver = webdriver.Firefox()
driver.get("https://www.example.com")
print(driver.title)
driver.quit()
This works like a clock on a local Windows machine but on a headless Ubuntu VPS returns this error
Traceback (most recent call last):
File "test.py", line 6, in <module>
driver = webdriver.Firefox(options=options)
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/firefox/webdriver.py", line 69, in __init__
super().__init__(command_executor=executor, options=options) File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/webdriver.py", line 208, in __init__
self.start_session(capabilities)
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/webdriver.py", line 292, in start_session
response = self.execute(Command.NEW_SESSION, caps)["value"]
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/webdriver.py", line 347, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/errorhandler.py", line 229, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 1
Where did I go wrong? Help much appreciated.
It looks like you're incorrectly setting headless mode. And you appear to be spinning up the browser twice (once without options). Use this for setting headless mode correctly:
Your full script would look like this: