在Raspberry Pi无头上使用Selenium

时间:2014-07-30 01:20:25

标签: python selenium raspberry-pi iceweasel

这是我第一次尝试使用Iceweasel浏览器在树莓派上运行Selenium。 我今天晚上尝试了一个简单的测试

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

我在运行时得到的错误是:

=====================================================================
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"

据我所知,我在网上看到的是Iceweasel充当了pi的Firefox替代品,许多人声称你所要做的就是调用firefox webdriver来使用它。 我只是做错了吗?

感谢您的时间。

2 个答案:

答案 0 :(得分:34)

这对Raspberry Pi无头无效:

安装:

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

代码:

from selenium import webdriver
from pyvirtualdisplay import Display

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

driver = webdriver.Firefox()

答案 1 :(得分:0)

我不确定为什么会这样,但是你得到的错误与Firefox驱动程序有关,使用“本机事件”进行用户交互模拟(键盘,鼠标等)。

有关原生事件的一些技术细节和背景/问题,请参阅: https://code.google.com/p/selenium/wiki/NativeEventsOnLinux

许多硒用户(包括我自己)发现“原生事件”在许多情况下都存在问题,而使用“合成事件”则更容易/更安全。合成事件通过JavaScript模拟用户交互。

所以,尝试在驱动程序中禁用本机事件(通过设置配置文件属性),您应该通过该错误。

示例:

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.