19

This is my first time trying to run Selenium on a raspberry pi using the Iceweasel browser. I tried a simple test this evening

# selenium test for /mod2 
# verify: posts, and page name
class TestMod2Selenium(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()

    def test_validate_page_elements(self):
        driver = self.driver
        driver.get("127.0.0.1:5000/mod2")
        self.assertIn("Home - microblog", driver.title)
    def tearDown(self):
        self.driver.close()

the error I get back at runtime is:

=====================================================================
ERROR: test_validate_page_elements (__main__.TestMod2Selenium)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test.py", line 58, in setUp
    self.driver = webdriver.Firefox()
  File "/home/pi/naughton_python/flask/flask/local/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 59, in __init__
    self.binary, timeout),
  File "/home/pi/naughton_python/flask/flask/local/lib/python2.7/site-packages/selenium/webdriver/firefox/extension_connection.py", line 47, in __init__
    self.binary.launch_browser(self.profile)
  File "/home/pi/naughton_python/flask/flask/local/lib/python2.7/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 61, in launch_browser
    self._wait_until_connectable()
  File "/home/pi/naughton_python/flask/flask/local/lib/python2.7/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 100, in _wait_until_connectable
    self._get_firefox_output())
WebDriverException: Message: "The browser appears to have exited before we could connect. The output was: ERROR: ld.so: object 'x_ignore_nofocus.so' from LD_PRELOAD cannot be preloaded: ignored.\nERROR: ld.so: object 'x_ignore_nofocus.so' from LD_PRELOAD cannot be preloaded: ignored.\nERROR: ld.so: object 'x_ignore_nofocus.so' from LD_PRELOAD cannot be preloaded: ignored.\nError: no display specified\n"

As I understand it from what I have read online is that Iceweasel acts as a Firefox replacement on the pi, and many have claimed that all you have to do is call the firefox webdriver to use it. Am I just doing this incorrectly?

Thank you for your time.

fivef
  • 2,397
  • 21
  • 21
Lombax
  • 851
  • 4
  • 9
  • 25

3 Answers3

39

This works for me on Raspberry Pi headless:

Installation:

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

Code:

from selenium import webdriver
from pyvirtualdisplay import Display

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

driver = webdriver.Firefox()
fivef
  • 2,397
  • 21
  • 21
  • great solution, also for anyone who is having issues with finding the correct geckodriver for their raspberry pi4, the lastest release of the firefox webdriver that works for me is v0.23 follow this link: https://github.com/mozilla/geckodriver/releases/tag/v0.23.0, it is the file geckodriver-v0.23.0-arm7hf.tar.gz – Ramces Gonzalez Jun 08 '20 at 01:36
1

I'm not sure why it is happening, but that error you are getting has to do with the Firefox driver using "native events" for user interaction simulation (keyboard, mouse, etc).

For some technical details and background/issues with native events, see: https://code.google.com/p/selenium/wiki/NativeEventsOnLinux

Many selenium users (myself included) find that "native events" are problematic in many situations, and it's just easier/safer to use "synthesized events" instead. Synthesized events emulate user interaction via JavaScript.

so, try disabling native events (by setting the profile property) in your driver and you should get past that error.

Example:

from selenium import webdriver

profile = webdriver.FirefoxProfile()
profile.native_events_enabled = False
driver = webdriver.Firefox(profile)
# synthesized events are now enabled for this 
# driver instance... native events are disabled.
Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
  • hi Corey, so things have "improved" and by that I mean a new error is getting thrown after making the the change you suggested. I now get the following at runtime: http://pastebin.com/2ZKKbCj6 – Lombax Aug 07 '14 at 02:26
  • 1
    ah.. ok. so you are trying to run headless (no display attached), this can be done with Xvfb and PyVirtualDisplay... see here for an example: http://coreygoldberg.blogspot.com/2011/06/python-headless-selenium-webdriver.html – Corey Goldberg Aug 07 '14 at 05:51
1

I was following @fivef's answer and after struggling with the compilation of geckodriver for new versions of Firefox, I decided to give a try to chomedriver and chromium and it was really easy:

sudo apt-get install chromium-chromedriver xvfb python-pip
sudo pip install pyvirtualdisplay selenium

and then in python:

from selenium import webdriver
from pyvirtualdisplay import Display

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

driver = webdriver.Chrome()
Pablo Guerrero
  • 936
  • 1
  • 12
  • 22