在 Raspberry Pi 上使用 Selenium 无头 [英] Using Selenium on Raspberry Pi headless

查看:52
本文介绍了在 Raspberry Pi 上使用 Selenium 无头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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"

据我了解,Iceweasel 在 pi 上充当 Firefox 的替代品,许多人声称您只需调用 firefox webdriver 即可使用它.我只是做错了吗?

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?

感谢您的时间.

推荐答案

这对我在 Raspberry Pi 无头:

This works for me on Raspberry Pi headless:

安装:

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

这篇关于在 Raspberry Pi 上使用 Selenium 无头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆