[英]Using Selenium on Raspberry Pi headless
This is my first time trying to run Selenium on a raspberry pi using the Iceweasel browser.这是我第一次尝试使用 Iceweasel 浏览器在树莓派上运行 Selenium。 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.据我在网上阅读的内容了解到,Iceweasel 在 pi 上充当 Firefox 的替代品,许多人声称您所要做的就是调用 firefox webdriver 来使用它。 Am I just doing this incorrectly?我只是做错了吗?
Thank you for your time.感谢您的时间。
This works for me on Raspberry Pi headless: 这适用于我的Raspberry Pi无头:
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()
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). 我不确定为什么会这样,但是你得到的错误与Firefox驱动程序有关,使用“本机事件”进行用户交互模拟(键盘,鼠标等)。
For some technical details and background/issues with native events, see: https://code.google.com/p/selenium/wiki/NativeEventsOnLinux 有关本机事件的一些技术细节和背景/问题,请参阅: 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. 合成事件通过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.
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:我一直在关注@fivef 的回答,在努力为 Firefox 的新版本编译 geckodriver 之后,我决定尝试使用 chomedriver 和 chromium,这真的很简单:
sudo apt-get install chromium-chromedriver xvfb python-pip
sudo pip install pyvirtualdisplay selenium
and then in python:然后在 python 中:
from selenium import webdriver
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start()
driver = webdriver.Chrome()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.