没有任何桌面浏览器的Selenium webdriver

时间:2013-02-06 11:57:16

标签: python selenium command-line selenium-webdriver htmlunit

我正在使用硒。我有以下代码:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
browser = webdriver.Firefox()
browser.get("http://www.mysite.com")
x = browser.find_elements_by_xpath("//div[@id='containeriso3']/div/a[1]")
hrefs = [i.get_attribute('href') for i in x]

现在,这有效。

但我想这样做是在运行ubuntu命令行的服务器上。这意味着我不能使用这个

browser = webdriver.Firefox()

在我的代码中。通过命令行可以使用哪种替代方法?

3 个答案:

答案 0 :(得分:4)

你可以使用HtmlUnitDriver,它是基于Rhino javascript引擎的无头浏览器。

http://code.google.com/p/selenium/wiki/HtmlUnitDriver

答案 1 :(得分:1)

如果您的Ubuntu服务器和桌面在同一网络上,请使用Selenium Grid。您的代码将在Linux服务器上启动,您的测试将在桌面上执行。

请看以下链接:

http://code.google.com/p/selenium/wiki/Grid2

示例是Java中的,但我确信您可以将它们调整为Python,或者至少可以了解您需要做什么。

答案 2 :(得分:0)

我认为你也可以尝试使用基于phantomjs的幽灵驱动程序:

https://github.com/detro/ghostdriver,或者您也可以尝试在Xvfb上运行常用的firefox驱动程序。

这取决于你需要什么。