1

Curious, does anyone here know how I might fix this Selenium/Ubuntu22 bug?

Got a Python3 script that still runs fine on Debian 12, but I'm trying to use it on Ubuntu22 with firefox and geckodriver being a snap now

The original code:

class search_jobs():
  def setup_method(self, method):
    self.driver = webdriver.Firefox()
    self.vars = {}

Generates:

$ python3 ./job-apply.py 
Do you want to skip ahead to the interactive job applications? (y/n): n
Traceback (most recent call last):
  File "/mnt/Piracy/job-apply/./job-apply.py", line 537, in <module>
    job_search.setup_method('Firefox')
  File "/mnt/Piracy/job-apply/./job-apply.py", line 70, in setup_method
    self.driver = webdriver.Firefox()
  File "/home/pcadmin/.local/lib/python3.10/site-packages/selenium/webdriver/firefox/webdriver.py", line 67, in __init__
    super().__init__(command_executor=executor, options=options)
  File "/home/pcadmin/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 206, in __init__
    self.start_session(capabilities)
  File "/home/pcadmin/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 290, in start_session
    response = self.execute(Command.NEW_SESSION, caps)["value"]
  File "/home/pcadmin/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 345, in execute
    self.error_handler.check_response(response)
  File "/home/pcadmin/.local/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: binary is not a Firefox executable

So I tried to make these explicit following some StackExchange advice:

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

class search_jobs():
  def setup_method(self, method):
    # Specify the path to geckodriver
    geckodriver_path = '/snap/bin/geckodriver'
    firefox_path = '/snap/bin/firefox'

    driver_service = Service(executable_path=geckodriver_path)

    options = Options()
    options.binary_location = firefox_path

    self.driver = webdriver.Firefox(service=driver_service, options=options)
    self.vars = {}

But it still generates the exact same bug:

$ python3 ./job-apply.py 
Do you want to skip ahead to the interactive job applications? (y/n): n
/mnt/Piracy/job-apply/./job-apply.py:72: DeprecationWarning: Firefox will soon stop logging to geckodriver.log by default; Specify desired logs with log_output
  driver_service = Service(executable_path=geckodriver_path)
Traceback (most recent call last):
  File "/mnt/Piracy/job-apply/./job-apply.py", line 545, in <module>
    job_search.setup_method('Firefox')
  File "/mnt/Piracy/job-apply/./job-apply.py", line 77, in setup_method
    self.driver = webdriver.Firefox(service=driver_service, options=options)
  File "/home/pcadmin/.local/lib/python3.10/site-packages/selenium/webdriver/firefox/webdriver.py", line 67, in __init__
    super().__init__(command_executor=executor, options=options)
  File "/home/pcadmin/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 206, in __init__
    self.start_session(capabilities)
  File "/home/pcadmin/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 290, in start_session
    response = self.execute(Command.NEW_SESSION, caps)["value"]
  File "/home/pcadmin/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 345, in execute
    self.error_handler.check_response(response)
  File "/home/pcadmin/.local/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: binary is not a Firefox executable
PC-Admin
  • 31
  • 4
  • Is the binary path valid? Can you try if you are able to open/start firefox via the path "/snap/bin/firefox" ? – Techrookie89 Aug 06 '23 at 16:59
  • Hi @Techrookie89, thanks for the reply. :) Yes I can open firefox using /snap/bin/firefox without issue. I also tried updating the Selenium pip3 package from 4.11.0 to 4.11.2 but the same error is produced. :S – PC-Admin Aug 07 '23 at 13:24

1 Answers1

2

The following code worked for me on Ubuntu 22.04:

  def setup_method(self, method):
    """Set up webdriver."""
    options = Options()
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')

    geckodriver_path = "/snap/bin/geckodriver"  # specify the path to your geckodriver
    driver_service = Service(executable_path=geckodriver_path)

    self.driver = webdriver.Firefox(options=options, service=driver_service)

    self.vars = {}

Something about these "options" seems to have magically un-jammed things. Thanks for reading! :)

PC-Admin
  • 31
  • 4
  • This solution helped me, too. But I don't know why it works even if I turn off the option after running it once. – john Aug 18 '23 at 16:38
  • For some reason setting the `geckodriver_path` made it work for me. I do not really understand this as `which geckodriver` points to the same path. – User Aug 19 '23 at 05:34