Selenium是否可以在没有GUI的Ubuntu上运行 [英] Will Selenium work on Ubuntu with no GUI

查看:115
本文介绍了Selenium是否可以在没有GUI的Ubuntu上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Amazon AWS和Cloud 9等各种云平台上使用Codeception。默认情况下,这两个都没有GUI。我的问题是,您可以在这种类型的系统上运行Selenium吗?还是我需要以某种方式获取GUI?

I'm using Codeception on various cloud platforms like Amazon AWS and Cloud 9. Neither of which has a GUI by default. My question is, can you run Selenium on this type of system? Or do I need to somehow get a GUI?

推荐答案

Selenium只是一个库,因此它并不特别在乎是否您正在配备GUI的系统上运行它。您可能要问的是:如果我使用Selenium打开浏览器,那么该浏览器是否可以在没有GUI的系统上运行。答案是:这取决于!

Selenium is only a library, and as such it does not particularly care if you are running it on a system that is equipped with a GUI. What you are probably asking is: If I use Selenium to open a browser, is that browser going to work on a system with no GUI. The answer to this is: it depends!

有无头浏览器:也没有GUI组件的浏览器。 HtmlUnit 与Selenium打包在一起。另一个流行的浏览器是 PhantomJS ,它具有名为 GhostDriver 。就我个人而言,我会避免两者! HtmlUnit使用的JavaScript引擎当前不支持任何桌面浏览器,因此测试不是很可靠。 GhostDriver已两年未维护,因此也会导致结果不可靠。 PahntomJS绝对是一种选择,因为它使用WebKit(Safari和Chrome浏览器中的引擎),但是您必须编写自己的 API

There are headless browsers: browsers that also do not have a GUI component. HtmlUnit is packaged with Selenium. Another popular browser is PhantomJS, which has third-party Selenium bindings library called GhostDriver. Personally I would avoid both of these! HtmlUnit uses a JavaScript engine that none of the current desktop browsers support, and as such the tests are not very reliable. GhostDriver has not been maintained for 2 years, and as such also makes for unreliable results. PahntomJS is definitely an option, as it uses WebKit - the engine that is in Safari and Chrome browsers, but you would have to write your own API.

大多数系统将允许您使用虚拟GUI。您提到了Ubuntu,它是Debian的衍生产品。网上有几本教程告诉您如何安装Xvfb,其中大多数教程不完整或错误。在Debian上,您可以安装如下所示的无头浏览器:

Most systems will allow you to have a virtual GUI. You mentioned Ubuntu, which is a Debian derivative. There are several tutorials on the Net that tell you how to install Xvfb, most of which are incomplete or wrong. On a Debian you install a headless browser like this:


  1. 安装Xvfb: apt-get install xvfb

  2. 安装浏览器。假设您使用的是Debian服务器,您将无法安装带有apt-get的Firefox之类的文件,因为不存在存储库。取而代之的是Google之类的 Firefox离线安装之类的东西,或者您要使用的任何浏览器,然后在服务器上使用 wget 来获取软件包。

  3. 解压软件包,如 / usr / local / lib ,然后从 / usr / local / bin创建一个软链接到启动浏览器的二进制文件。

  4. 现在尝试无头启动浏览器。例如,对于Firefox,您可以尝试: xvfb运行firefox 。这可能会产生一些错误,您必须修复。就我而言,我缺少库 libdbus-glib-1-2 ,只需使用apt-get即可安装。

  5. 此时,您将需要在运行Selenium测试之前启动Xvfb。大多数CI服务器都有Xvfb的插件,或者您可以从命令行执行: Xvfb:99& 。有关其他信息,请参见 docs

  1. Install Xvfb: apt-get install xvfb
  2. Install a browser. Assuming you are using a Debian server, you will not be able to install something like Firefox with apt-get, because the repositories are not present. Instead Google something like "Firefox off-line install", or whatever browser you want to use, and then use wget on your server to grab the package.
  3. Unpack the package somewhere like /usr/local/lib, and then create a soft link from /usr/local/bin to the binary that launches the browser.
  4. Now try launching your browser headless. For example, for Firefox you would try: xvfb-run firefox. This may produce some errors, which you have to fix. In my case, I was missing the library libdbus-glib-1-2 which I could install just using apt-get.
  5. At this point you will need to launch Xvfb before running your Selenium tests. Most CI servers have a plugin for Xvfb, or you can do it from the command line: Xvfb :99 &. See the docs for additional information.

这篇关于Selenium是否可以在没有GUI的Ubuntu上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆