如何在Web服务器中运行Selenium Scripts?

时间:2014-02-04 13:24:19

标签: python testing selenium selenium-webdriver

我已经编写了一些Selenium [Python] webdriver脚本,虽然它们在我的系统上运行良好,但它们并没有在我的网站服务器上运行。它显示Firefox的错误。 Firefox安装在服务器上。网络服务器是Ubuntu。我该怎么做才能运行这些脚本?请帮忙,我是新手。

4 个答案:

答案 0 :(得分:1)

Selenium需要运行浏览器,浏览器需要某种X服务器才能运行。有许多类型的X服务器,其中一个是Xvfb又是X虚拟帧缓冲区,可以在内存中执行所有操作,因此不需要屏幕。

在维基百科中,您可以找到非常nice examples

This is a nice example

答案 1 :(得分:0)

在服务器上执行脚本时,您可能需要打开浏览器。

这是Firefox的Java代码(Python代码应该类似):

import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxBinary;

WebDriver openHeadless() throws Exception
{
    FirefoxBinary binary = new FirefoxBinary(new File("/usr/local/bin/firefox"));
    binary.setEnvironmentProperty("DISPLAY",System.getProperty("lmportal.xvfb.id",":99"));
    return new FirefoxDriver(binary,null); // or 'binary,profile' if you have a profile
}

确保在/usr/local/bin/firefox处的服务器上安装了Firefox。

答案 2 :(得分:0)

如果您只想进行网络浏览器测试,可以使用像Casper JS这样的库来创建用于Web浏览器测试的服务器端浏览器,它不需要显示驱动程序。

答案 3 :(得分:0)

我最终使用pyvirtualdisplay,这是Xvfb和Xephyr的Python包装器。如果有人遇到同样的问题[我相信新手会有],你可以试试THIS。您也可以使用xvfbwrapper。 xvfbwrapper HERE的教程。