硒在覆盆子皮无头上的应用

2024-06-23 20:13:08 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我第一次尝试使用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的替代品,很多人都声称你所要做的就是调用FirefoxWebDriver来使用它。 我这样做是不是不对?

谢谢你抽出时间。


Tags: inpytestselfflaskhomesodriver
2条回答

这对我来说很管用

安装:

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.

相关问题 更多 >

    热门问题