Selenium WebDriver PYTHON selenium.common.exceptions.WebDriverException:

0 投票
1 回答
2062 浏览
提问于 2025-04-18 03:07

这是我的代码:

 from selenium import webdriver
 from selenium.webdriver.common.keys import Keys

 browser = webdriver.Firefox()

 browser.get('http://www.yahoo.com')
 assert 'Yahoo!' in browser.title

 elem = browser.find_element_by_name('p')  # Find the search box
 elem.send_keys('seleniumhq' + Keys.RETURN)

 browser.quit()

但是我遇到了一个错误:

        root@debian:~# python python_org_search.py
        Traceback (most recent call last):
          File "python_org_search.py", line 4, in <module>
            browser = webdriver.Firefox()
          File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/firefox/webdriver.py", line 59, in __init__
            self.binary, timeout),
          File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/firefox/extension_connection.py", line 47, in __init__
            self.binary.launch_browser(self.profile)
          File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 61, in launch_browser
            self._wait_until_connectable()
          File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 100, in _wait_until_connectable
            self._get_firefox_output())
        selenium.common.exceptions.WebDriverException: Message: 'The browser appears to have exited before we could connect. The output was: Error: no display specified\n'

这个错误是因为我在使用一个命令行界面的操作系统(DEBIAN服务器6.0.1)吗?

1 个回答

1

大多数浏览器需要在一个叫做 x display 的环境中运行。为了实现这一点,我使用了一个叫做 xvfb 的工具(可以通过命令 apt-get install xvfb 来安装)。

接下来,Python 需要能够使用这个显示环境,你可以通过这个链接找到相关的工具:https://pypi.python.org/pypi/PyVirtualDisplay

先启动这个显示环境,然后初始化 webdriver 浏览器,就可以开始测试了……

撰写回答