这是我第一次尝试使用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 specifiedn"
我从网上了解到,Iceweasel在pi上充当Firefox的替代品,许多人声称你所要做的就是调用Firefox的web驱动程序来使用它。是我做错了吗?
谢谢你的时间。
这适用于我的树莓派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()
我不确定为什么会发生这种情况,但是你得到的错误与Firefox驱动程序使用"本地事件"进行用户交互模拟(键盘,鼠标等)有关。
有关本地事件的一些技术细节和背景/问题,请参见:https://code.google.com/p/selenium/wiki/NativeEventsOnLinux
许多selenium用户(包括我自己)发现"本地事件"在许多情况下都是有问题的,而使用"合成事件"更容易/更安全。合成事件通过JavaScript模拟用户交互。
因此,尝试禁用驱动程序中的本机事件(通过设置profile属性),您应该可以解决该错误。
的例子:
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.
我跟随@fivef的回答,在为新版本的Firefox编译geckodriver之后,我决定尝试一下chomedriver和chromium,这真的很容易:
sudo apt-get install chromium-chromedriver xvfb python-pip
sudo pip install pyvirtualdisplay selenium
,然后在python中:
from selenium import webdriver
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start()
driver = webdriver.Chrome()