19

这是我第一次尝试使用 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 来使用它。我只是做错了吗?

感谢您的时间。

4

2 回答 2

38

这适用于无头树莓派:

安装:

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()
于 2014-09-08T14:00:35.817 回答
1

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

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

许多 selenium 用户(包括我自己)发现“本机事件”在许多情况下都是有问题的,而使用“合成事件”更容易/更安全。合成事件通过 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.
于 2014-08-06T18:10:37.740 回答