Alex Gisi
Alex Gisi

Reputation: 93

selenium WebDriverException: "connection refused" occuring on headless Pi

Edit: This does not seem to be a duplicate because none of the suggestions in the answers of the post linked fix the error. Here is the geckodriver.log after running the script:

1538960169585   mozrunner::runner       INFO    Running command: "/usr/bin/firefox" "-marionette" "--headless" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofile.xicOi27i6laa"
1538960178656   Marionette      INFO    Listening on port 2828
^G[Child 17792] ###!!! ABORT: Aborting on channel error.: file /build/firefox-esr-YKrXxr/firefox-esr-52.9.0esr/ipc/glue/MessageChannel.cpp, line 2152
[Child 17792] ###!!! ABORT: Aborting on channel error.: file /build/firefox-esr-YKrXxr/firefox-esr-52.9.0esr/ipc/glue/MessageChannel.cpp, line 2152

I'm unable to create webdriver instance running python 3.5 on a headless Raspberry Pi 3 B+. I have run

sudo apt-get install python-pip iceweasel xvfb pip install pyvirtualdisplay selenium

to install dependencies. When I run a basic script to create a selenium WebDriver, though, I get a WebDriverException: Message: connection refused message.

My code:

from selenium import webdriver
from pyvirtualdisplay import Display

display = Display(visible=0, size=(800, 600))
display.start()

profile = webdriver.FirefoxProfile()
profile.native_events_enabled = False

driver = webdriver.Firefox(profile)

Error message:

 Traceback (most recent call last):
  File "simpletest", line 10, in <module>
    driver = webdriver.Firefox(firefox_profile=profile)
  File "/home/pi/.local/lib/python3.5/site-packages/selenium/webdriver/firefox/webdriver.py", line 174, in __init__
    keep_alive=True)
  File "/home/pi/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/home/pi/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 252, 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 321, 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

This answer from another forum indicates the above code should work. Any help is appreciated.

Upvotes: 0

Views: 874

Answers (1)

MJ Richardson
MJ Richardson

Reputation: 11

I encountered the same issue with my new Raspbery Pi 3 B+. I fixed the error by installing an old version of geckodriver. I am running Raspbian GNU/Linux 9 (stretch). You can look up your own operating system version if you run cat /etc/os-release in a bash terminal on your Raspberry Pi. You will get an output which looks like the following:

PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

If you are using Raspbian, then when you installed Firefox you probably received version 52.9.0 or earlier. You can check this by running firefox-esr -version from a bash terminal on your Raspberry Pi. Note that iceweasel is really firefox-esr. See https://lwn.net/Articles/676799/ for more details. Version 52.9.0 is the most recent, fully supported version for Raspbian and even if you run sudo apt-get update or sudo apt-get upgrade firefox-esr the version will not change. In fact, sudo apt-get upgrade firefox-esr will tell you that firefox-esr is already the newest version (52.9.0esr-1~deb9u1).

Since you cannot upgrade Firefox, you need to install an old version of geckodriver. On the page https://github.com/mozilla/geckodriver/releases/, it recommends Firefox 55.0 and greater and Selenium 3.5 and greater for geckodriver v0.19.0. You should therefore download and install geckodriver v0.18.0. The following commands should do the trick:

curl -O https://github.com/mozilla/geckodriver/releases/download/v0.18.0/geckodriver-v0.18.0-arm7hf.tar.gz
tar -xzvf geckodriver-v0.18.0-arm7hf.tar.gz
sudo cp geckodriver /usr/local/bin/

Note: you may need to run sudo chmod +x /usr/local/bin/geckodriver if you are receiving a permissions error.

Upvotes: 1

Related Questions