【问题标题】:How to run Selenium Scripts in webservers?如何在网络服务器中运行 Selenium 脚本?
【发布时间】:2014-02-28 11:56:44
【问题描述】:

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

【问题讨论】:

    标签: python testing selenium selenium-webdriver


    【解决方案1】:

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

    在 Wikipedia 中,您可以找到 nice examples

    This is a nice example 也是。

    【讨论】:

      【解决方案2】:

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

      这里是 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。

      【讨论】:

        【解决方案3】:

        如果您只想进行网络浏览器测试,您可以使用 Casper JS 之类的库,这将为网络浏览器测试创建一个服务器端浏览器,它不需要显示驱动程序。

        【讨论】:

          【解决方案4】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-01-01
            • 2019-08-27
            • 1970-01-01
            • 2015-06-11
            • 2016-11-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多