JavaEar 专注于收集分享传播有价值的技术资料

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.

2个回答

    最佳答案
  1. 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
    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.

  2. 参考答案2
  3. Please start with checking your "firefox" browser version.

    I found it very confusing at some point. I'm using the Raspbian and the "Iceweasel" downloaded with apt-get was a Firefox 52 version which didn't work with geckodriver 0.19 (this one requires Firefox 55 or greater).

    What worked for me was to download geckorvider v0.16 and that resolved the problem.

    Whats moreover, you probably don't need xorg to make it work, the only packages I needed was xfvb and iceweasel.