Java Selenium Webdriver - FirefoxDriver 问题:错误:无法打开显示:0.0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21827531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:55:34  来源:igfitidea点击:

Selenium Webdriver - Issue with FirefoxDriver: Error: cannot open display: :0.0

javaseleniumjunit

提问by Dan

I made a test case in selenium which automatically opens Firefox and do its login stuffs. I made this using java programming through Eclipse.& i tested it is working fine in my windows7 system.

我在 selenium 中做了一个测试用例,它会自动打开 Firefox 并进行登录。我通过 Eclipse 使用 java 编程完成了这个。我测试它在我的 windows7 系统中工作正常。

And Now, the problem isA cron job starts this same program in the server, which throws the following error when trying to open firefox:

现在,问题是一个 cron 作业在服务器中启动了相同的程序,它在尝试打开 Firefox 时抛出以下错误:

Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
 ------------- Standard Error -----------------
 org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055       after 45000 ms. Firefox console output:
 Error: cannot open display: :0.0
 Error: cannot open display: :0.0

    at             org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnectio n.java:118)
    at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:250)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:110)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:197)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:190)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:186)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:99)
    at com.lo.test.selenium.AssignCampaignTestCase.<clinit>(AssignCampaignTestCase.java:42)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:266)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:375)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeInVM(JUnitTask.java:1420)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:848)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeOrQueue(JUnitTask.java:1899)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:800)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:390)
    at org.apache.tools.ant.Target.performTasks(Target.java:411)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
    at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
    at org.apache.tools.ant.Main.runBuild(Main.java:809)
    at org.apache.tools.ant.Main.startAnt(Main.java:217)
    at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
    at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
  ------------- ---------------- ---------------

    Caused an ERROR
 null

I have installed xvfb & selenuim in my server. I checked they are running. And i followed this tutorial

我已经在我的服务器中安装了 xvfb 和 selenuim。我检查他们正在运行。我跟着这个教程

& my testCase Example is

&我的测试用例示例是

        package com.lo.test.selenium; 
    import static org.junit.Assert.assertTrue;
    import static org.junit.Assert.fail;

    import java.util.Properties;
    import java.util.ResourceBundle;

    import javax.mail.Flags;
    import javax.mail.Folder;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.NoSuchProviderException;
    import javax.mail.Session;
    import javax.mail.Store; 
    import org.junit.Test;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.Select;
    import org.openqa.selenium.support.ui.WebDriverWait;

   public class MyExampleTestCase {

  private static ResourceBundle rb = ResourceBundle.getBundle("global-messages");
  static WebDriver driver = new FirefoxDriver();  //Make it static in order to make one    
      instance of this class(helps to avoid opening of 2 browsers at once)

  @Test

  public void campaignEmailTestCase() throws InterruptedException { 
 MyExampleTestCase emTesObj=new MyExampleTestCase ();  
 String fName="test LO";
    Integer LeadId=570903;
    String campaignName="2Get"; 
    String SubjName="Welcome";   //NOTE: No problem,Type without 
   double quotes. It will work for you 
    emTesObj.doLogin(); 
    emTesObj.goToLeadsListPage();
    emTesObj.searchByFirstName(fName);//without this also,it will work but Gives you the correct 
     navigation on UI
    emTesObj.waitBeforePageLoads(LeadId.toString()); //pass the Id attribute which you aspect to 
     see after the page is loaded
    emTesObj.assignCampaign(LeadId, campaignName);  
    emTesObj.readRecentEmail(SubjName);  
}

采纳答案by barak manos

Use this when you run your code on a local machine:

当你在本地机器上运行你的代码时使用这个:

private WebDriver driver = null;

public void open() throws Exception
{
    driver = new FirefoxDriver();
}

And this when you run your code on a remote server:

当您在远程服务器上运行代码时:

private WebDriver driver = null;

public void openHeadless() throws Exception
{
    FirefoxBinary binary = new FirefoxBinary(new File("/usr/local/bin/firefox"));
    binary.setEnvironmentProperty("DISPLAY",System.getProperty("lmportal.xvfb.id",":99"));
    driver = new FirefoxDriver(binary,null);
}

回答by vania-pooh

If you're running Selenium node in headless mode with xvfb-run command like the following...

如果您在无头模式下使用 xvfb-run 命令运行 Selenium 节点,如下所示...

$ xvfb-run -a -s "-screen 0 1280x1600x24" java -jar selenium-server-standalone.jar -role node

...then you may encounter the same symptoms because of xvfb process crash. Here's how the process tree looks like in this case:

...那么您可能会因为xvfb 进程崩溃而遇到相同的症状。在这种情况下,流程树如下所示:

├─sh -e /proc/self/fd/9
│   └─xvfb-run /usr/bin/xvfb-run -a -s -screen 0 1280x1600x24 java -jar selenium-server-standalone.jar -role node
│       ├─Xvfb :104 -screen 0 1280x1600x24
│       │   └─4*[{Xvfb}]
│       └─java -jar selenium-server-standalone.jar -role node
│           └─27*[{java}]

Xvfb process in this tree by default restarts for each browser launch (see -resetoption documentation) and for some reason has memory leaks. So when you use the node during days and hours memory leaks cause Xvfb process to crash but java process with Selenium node stays intact. In this situation browser being launched by Selenium can't open display because xvfb is not running and you're seeing the error message.

默认情况下,此树中的 Xvfb 进程会在每次浏览器启动时重新启动(请参阅-reset选项文档),并且由于某种原因存在内存泄漏。因此,当您在几天和几小时内使用该节点时,内存泄漏会导致 Xvfb 进程崩溃,但带有 Selenium 节点的 java 进程保持完整。在这种情况下,Selenium 启动的浏览器无法打开显示,因为 xvfb 没有运行并且您看到了错误消息。

To solve the issue you need to turn off xvfb restart behavior which is done by -noresetoption:

要解决此问题,您需要关闭由-noreset选项完成的xvfb重启行为:

$ xvfb-run -a -s "-screen 0 1280x1600x24 -noreset" java -jar selenium-server-standalone.jar -role node

回答by Daniel S.

If you are using selenium with maven ,just update dependencies in the pom.xml file and it will fix the issue. It worked for me. If you are using test unit,update the jars used to the latest version :)

如果您在 maven 中使用 selenium,只需更新 pom.xml 文件中的依赖项即可解决问题。它对我有用。如果您使用的是测试单元,请将使用的 jar 更新到最新版本:)