【发布时间】:2014-02-28 11:56:44
【问题描述】:
我编写了一些 Selenium [Python] webdriver 脚本,虽然它们在我的系统上运行良好,但它们并没有在我的网站服务器上运行。它显示 Firefox 的错误。 Firefox 安装在服务器上。网络服务器是 Ubuntu。我必须做什么才能运行这些脚本?请帮忙,我是新手。
【问题讨论】:
标签: python testing selenium selenium-webdriver
我编写了一些 Selenium [Python] webdriver 脚本,虽然它们在我的系统上运行良好,但它们并没有在我的网站服务器上运行。它显示 Firefox 的错误。 Firefox 安装在服务器上。网络服务器是 Ubuntu。我必须做什么才能运行这些脚本?请帮忙,我是新手。
【问题讨论】:
标签: python testing selenium selenium-webdriver
Selenium 需要一个正在运行的浏览器,而浏览器需要某种 X 服务器才能运行。有许多类型的 X 服务器,其中一种是 Xvfb 又名 X 虚拟帧缓冲区,它在内存中执行所有操作,因此不需要屏幕。
在 Wikipedia 中,您可以找到 nice examples。
【讨论】:
在服务器上执行脚本时,您可能需要无头打开浏览器。
这里是 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。
【讨论】:
如果您只想进行网络浏览器测试,您可以使用 Casper JS 之类的库,这将为网络浏览器测试创建一个服务器端浏览器,它不需要显示驱动程序。
【讨论】: