16

Here is my code:

from selenium import webdriver

browser = webdriver.Firefox()

browser.get('http://www.python.org')

browser.close()

It launched the firefox browser when I ran this script, but the page is blank, then the command line shows the error message:

Traceback (most recent call last):
  File "ad.py", line 3, in <module>
    browser = webdriver.Firefox()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 76, in __init__
    keep_alive=True)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 92, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 179, in start_session
    response = self.execute(Command.NEW_SESSION, capabilities)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: connection refused

My python vesion is 2.7.3 and the selenium version is selenium-3.0.0.b3.egg-info

Please, how do I solve the problem ...

3
  • Its working fine for me. Are you behind proxy?
    – Jeril
    Commented Sep 17, 2016 at 14:12
  • No,never behinnd a proxy.
    – leven
    Commented Sep 19, 2016 at 1:33
  • have you managed to solve the problem @leven
    – Timir
    Commented Oct 26, 2016 at 10:13

7 Answers 7

17

Check your geckodriver.log file (should be in the same directory as python file)

If it says Error: GDK_BACKEND does not match available displays then install pyvirtualdisplay:

pip install pyvirtualdisplay selenium

You might need xvfb too:

sudo apt-get install xvfb # Debian

sudo yum install Xvfb # Fedora

Then try adding this code:

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

Full example:

from pyvirtualdisplay import Display
from selenium import webdriver

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

browser = webdriver.Firefox()
browser.get('http://www.python.org')

browser.close()
2
  • 1
    I can confirm that using a virtual display like this fixed the problem for me.
    – exhuma
    Commented Dec 6, 2016 at 9:41
  • 1
    worked for me but need to use Xvfp on Centos (capital X)
    – JMG
    Commented Jul 27, 2017 at 22:25
4

As mentioned by @kervvv this issue is probably related to an older version of Firefox than what the version of selenium and/or geckodriver expect(s) or need(s). It should be noted, as far as I can tell, that the specific error message from selenium is somewhat generic or vague; so, it doesn't explicitly show why the error occurs.

In case users are here looking for help while using an older version of Firefox, including the Extended Support Release (ESR), the following solution should work just fine.

  1. Visit the Firefox download page to download a Beta, Nightly, or Developer version of Firefox.
  2. Extract the package into an arbitrary location on your filesystem (anywhere you want)
  3. Specify the FirefoxBinary within your code or script to point to the downloaded location.

    from selenium import webdriver
    from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
    binary = FirefoxBinary('/home/username/firefox/firefox')
    driver = webdriver.Firefox(firefox_binary=binary)
    driver.get(url)
    

That works for me on Gentoo, for example, where the version of geckodriver (0.20.0) and selenium (3.11.0) are the latest available upstream, while Firefox (ESR) is at version 52.

3

Had this problem as well. Needed to set the DISPLAY. For me Xvfb frame buffer is running on local machine at :99 so.

$ export DISPLAY=:99
0
3

Had the same issue. Thought it was proxy or port related (to no avail) but what solved my problem was just simply updating Firefox. I was running 52.0.xxx and updated to 57.0.2. Link here.

2

This could be for various reasons.

  • Most probably because the "latest" version of your geckodriver is not able to communicate with your "slightly older" firefox.

  • The easiest way to fix this would be to try out different older versions of geckodriver. Run the following command to find the current version of your geckodriver

    geckodriver --version
    
  • If it shows the version as 19 or above, execute following steps to use geckodriver version 17(Works 90% of times)

    1. Your existing geckodriver most typically may be placed in /usr/local/bin when u installed it earlier. First delete this by running sudo rm -r /usr/local/bin/geckodriver

    2. Download version 17 of geckodriver from this link. Move the downloaded file(geckodriver-v0.17.0-arm7hf.tar.gz) from your Downloads folder into your home directory

    3. Unpack the file

      tar -xzvf geckodriver-v0.17.0-arm7hf.tar.gz
      

      This will create a folder called "geckodriver" in your home directory

    4. Move/Copy this extracted "geckodriver" into /usr/local/bin/

      sudo cp geckodriver /usr/local/bin/
      
    5. Run

      sudo reboot
      

Rerun your program now...
It should work!

0
2

First thing to do: Update Firefox and make sure you have the latest version of geckodriver installed (https://github.com/mozilla/geckodriver/releases)

0

I got the same error. After updating the geckodriver vresion to geckodriver 0.24.0 ( 2019-01-28) worked fine for me.

Download source :https://github.com/mozilla/geckodriver/releases/download/v0.24.0/geckodriver-v0.24.0-linux32.tar.gz

1
  • 1
    0.29.1 have the same problem. Commented Jun 10, 2021 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.