Search code examples
pythonseleniumselenium-webdriverxvfbgeckodriver

Python Selenium Geckodriver Connection refused


I spent hours trying to make Selenium works with Python no luck this error message selenium.common.exceptions.WebDriverException: Message: connection refused this is the example I have used:-

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()

This is depence I intalled apt-get install -y xorg xvfb dbus-x11 xfonts-100dpi xfonts-75dpi xfonts-cyrillic

This is /root/geckodriver.log output

1493938773101 geckodriver INFO Listening on 127.0.0.1:40876 1493938774156 geckodriver::marionette INFO Starting browser /usr/lib/firefox/firefox.sh with args ["-marionette"] (firefox:3128): GLib-GObject-CRITICAL **: g_object_ref: assertion 'object->ref_count > 0' failed

I'm running Selenium on Ubuntu 14.04 64-bit VPS remote server with 128MB RAM I can't figure out what's make Selenium not able to communicate with browsers drivers both Chrome and Firefox.


Solution

  • Ok, I gave up on Geckodriver and I use PhantomJS as my webdriver.

     from selenium import webdriver  
     display = Display(visible=0, size=(800, 600))
     display.start() 
     driver = webdriver.PhantomJS()
     driver.get('http://www.python.org')
     html_source = driver.page_source
     print ("html_source:",html_source) 
     driver.quit()
    

    Here are the steps I used to install PhantomJS :

    cd ~
    export PHANTOM_JS="phantomjs-2.1.1-linux-x86_64"
    wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
    tar xvjf $PHANTOM_JS.tar.bz2
    mv $PHANTOM_JS /usr/local/share
    ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/bin
    
    
    
    Python  Selenium        
    apt-get install python-pip  -y 
    pip uninstall pyvirtualdisplay
    apt-get install x11vnc xvfb fluxbox
    Xvfb :99 -ac
    xvfb-run  -a   python 99.py
    pip uninstall selenium
    pip install selenium==2.53.1
    

    See also How to install PhantomJS on Ubuntu.