这是我的代码:
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'
是否出现此错误,因为我使用的是CLI(命令行界面)操作系统(DEBIAN服务器6.0.1)
答案 0 :(得分:1)
大多数浏览器需要在x显示器中运行。我通过运行xvfb(apt-get install xvfb)来解决这个问题。
然后python需要能够使用这个显示:https://pypi.python.org/pypi/PyVirtualDisplay
启动显示,然后初始化webdriver浏览器并测试...