0

I am not able to run selenium.webdriver because of the following error:

selenium.common.exceptions.WebDriverException: Message: Service /pathtogeckodriver/ unexpectedly exited. Status code was: 1

Here is the code I am using:

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

s = Service(executable_path="/path/to/geckodriver")
driver = webdriver.Firefox(service=s)
driver.quit()

Python: 3.7.3 Selenium: 4.11.2 geckodriver: 0.23.0

maic0L
  • 45
  • 8

3 Answers3

0

Since you are on latest selenium(v4.11.2), you do not really have to pass the location of geckodriver and browser. Selenium can download and manage drivers for you.

Code can be simplified as:

from selenium import webdriver

driver = webdriver.Firefox()
driver.quit()

For more info, references:

Shawn
  • 4,064
  • 2
  • 11
  • 23
  • I also tried using this method, but without any success -> selenium.common.exceptions.NoSuchDriverException: Message: Unable to obtain driver for firefox using Selenium Manager.; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location – maic0L Aug 09 '23 at 13:23
  • Which OS do you use? Windows/Mac? – Shawn Aug 09 '23 at 13:35
  • Linux (Raspberry pi4b) – maic0L Aug 09 '23 at 13:41
  • Check this path `usr/local/bin/` delete the drivers from the cache. Then try to run your code, selenium should download the driver and store it in this path. Try that and let me know the status. – Shawn Aug 09 '23 at 13:44
  • usr/local/bin/ is completely empty – maic0L Aug 09 '23 at 13:50
0

The https://github.com/seleniumbase/SeleniumBase driver manager can handle compatibility issues across most platforms.

pip install seleniumbase, and this run this with python for Firefox tests:

from seleniumbase import Driver

driver = Driver(browser="firefox")
# ...
driver.quit()
Michael Mintz
  • 9,007
  • 6
  • 31
  • 48
  • It raises this error: OSError: [Errno 8] Exec format error: '/home/pi/.local/lib/python3.7/site-packages/seleniumbase/drivers/geckodriver' – maic0L Aug 09 '23 at 13:57
  • That looks like this: https://stackoverflow.com/questions/50662674/oserror-errno-8-exec-format-error-geckodriver-when-trying-to-open-firefox – Michael Mintz Aug 09 '23 at 14:01
  • Caused by an old version of geckodriver. `sbase get geckodriver --path` should resolve it. – Michael Mintz Aug 09 '23 at 14:02
  • Apparently seleniumbase saves geckodriver to a non-existing path ('/home/pi/.local/lib/python3.7/site-packages/seleniumbase/drivers/geckodriver') – maic0L Aug 09 '23 at 14:13
  • If it was saved there, how can the path not exist? It looks like a valid python package folder (site-packages). Drivers are by default scanned in that folder. If not there, then they are searched for on the system PATH. – Michael Mintz Aug 09 '23 at 14:18
  • You are right and I need some sleep. Anyway, when I ran sbase ... I get this error : PermissionError: [Errno 13] Permission denied: '/usr/local/bin/geckodriver'. – maic0L Aug 09 '23 at 14:34
  • That means that seleniumbase didn't have permission to copy geckodriver from your `/home/pi/.local/lib/python3.7/site-packages/seleniumbase/drivers/` folder to `/usr/local/bin/`. If you can't copy it directly yourself, use `sbase get geckodriver` without the `--path`. SeleniumBase will try to use the driver from `site-packages`. – Michael Mintz Aug 09 '23 at 14:39
  • I copied it myself to /usr/local/bin/ and now it has geckodriver in it. However it gives the same exec format error – maic0L Aug 09 '23 at 14:46
  • I think I figured it out. Raspberry PI expects a different driver from regular Linux - https://raspberrypi.stackexchange.com/a/101714 - Try getting a different one from https://github.com/mozilla/geckodriver/releases – Michael Mintz Aug 09 '23 at 14:53
  • After installing a diff version of geckodriver and copying to seleniumbase, I get the error of the main post (exited, status code 1). Guess I have to try with different geckodriver's versions and find the right one? – maic0L Aug 09 '23 at 15:13
  • 1
    One of those geckodrivers should work, based on other posts. Trial and error. Raspberry Pi is special. – Michael Mintz Aug 09 '23 at 15:23
0

GeckoDriver v0.23.0 is old and ancient now.

To use the recent version of e.g. v116.0.2 you need the latest version of GeckoDriver e.g. v0.33.0


However, as you are using you need to drop the keyword executable_path as per the discussion executable_path has been deprecated selenium python.

So your effective code will be:

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

s = Service("/path/to/geckodriver")
driver = webdriver.Firefox(service=s)
driver.quit()

Moreover, Selenium Manager is the new tool which is now integrated with Selenium v4.6 onwards that helps us to get a working environment to run Selenium out of the box. Beta 1 of Selenium Manager will configure the browser drivers for Chrome, Firefox, Edge, etc, browser clients, even if they are not present on the PATH.

So your minimal code can be:

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("https://www.google.com/")
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352