Search code examples
pythonseleniumfirefoxraspberry-pi3geckodriver

selenium.common.exceptions.WebDriverException: Message: connection refused while trying to open Firefox browser through GeckoDriver in Raspberry-pi3


Hi I have a raspberry pi 3 and decided to try get selenium running with python 3. I installed it using "pip3 install selenium" and there were no errors. After I made a small test script with this code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

# create a new Firefox session
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.maximize_window()

# Navigate to the application home page
driver.get("http://www.google.com")

# get the search textbox
search_field = driver.find_element_by_id("lst-ib")
search_field.clear()

# enter search keyword and submit
search_field.send_keys("Selenium WebDriver Interview questions")
search_field.submit()

# get the list of elements which are displayed after the search
# currently on result page using find_elements_by_class_name method
lists= driver.find_elements_by_class_name("_Rm")

# get the number of elements found
print ("Found " + str(len(lists)) + " searches:")

# iterate through each element and print the text that is
# name of the search

i=0
for listitem in lists:
   print (listitem.get_attribute("innerHTML"))
   i=i+1
   if(i>10):
      break

# close the browser window
driver.quit()

but it gives me an error:

Traceback (most recent call last):
  File "/home/pi/SeleniumTest.py", line 5, in <module>
    driver = webdriver.Firefox()
  File "/home/pi/.local/lib/python3.5/site-packages/selenium/webdriver/firefox/webdriver.py", line 170, in __init__
    keep_alive=True)
  File "/home/pi/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 156, in __init__
    self.start_session(capabilities, browser_profile)
  File "/home/pi/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 245, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/pi/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 314, in execute
    self.error_handler.check_response(response)
  File "/home/pi/.local/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: connection refused

Any help is appreciated, thank you :)


Solution

  • This error message...

    selenium.common.exceptions.WebDriverException: Message: connection refused
    

    ...implies that the GeckoDriver was unable to initiate/spawn a new Web Client session/instance successfully.

    The version information of the binaries you are using in-terms of might have given us some more insights into the error you are observing.

    However you may need to pass the Key executable_path along with the Value referring to the absolute path of the GeckoDriver as an argument while initializing the WebDriver and WebBrowser as follows :

    from selenium import webdriver
    
    # create a new Firefox session
    driver = webdriver.Firefox(executable_path=r'/path/to/geckodriver')
    

    Additional Recommendations

    • Upgrade Selenium to current levels Version 3.11.0.
    • Upgrade GeckoDriver to GeckoDriver v0.20.1 level.
    • Ensure GeckoDriver is present in the specified location as mentioned through the argument executable_path.
    • Ensure GeckoDriver is having executable permission for non-root users.
    • Upgrade Firefox version to Firefox v59.0.2 levels.
    • Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
    • Use CCleaner tool to wipe off all the OS chores before and after the execution of your Test Suite.
    • If your base Web Client version is too old, then uninstall it through Revo Uninstaller and install a recent GA and released version of Web Client.
    • Take a System Reboot.
    • Execute your Test as a non-root user.
    • Always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.