Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.net.ConnectException:在无头模式下调用Firefox浏览器时,未能连接到localhost/127.0.0.1:xxxx_Java_Maven_Selenium_Geckodriver_Firefox Headless - Fatal编程技术网

java.net.ConnectException:在无头模式下调用Firefox浏览器时,未能连接到localhost/127.0.0.1:xxxx

java.net.ConnectException:在无头模式下调用Firefox浏览器时,未能连接到localhost/127.0.0.1:xxxx,java,maven,selenium,geckodriver,firefox-headless,Java,Maven,Selenium,Geckodriver,Firefox Headless,我收到一个“java.net.ConnectException:无法连接到本地主机” 尝试初始化FirefoxDriver时出错。有什么帮助吗 代码和堆栈跟踪如下: Java代码 firefoxBinary = new FirefoxBinary(); firefoxBinary.addCommandLineOptions("--headless"); System.setProperty("webdriver.gecko.driver", (String) pluginConfig.get(

我收到一个“java.net.ConnectException:无法连接到本地主机” 尝试初始化FirefoxDriver时出错。有什么帮助吗

代码和堆栈跟踪如下:

Java代码

firefoxBinary = new FirefoxBinary();
firefoxBinary.addCommandLineOptions("--headless");

System.setProperty("webdriver.gecko.driver", (String) pluginConfig.get("geckodriver"));
firefoxOptions = new FirefoxOptions();

firefoxOptions.setBinary(firefoxBinary);
driver = new FirefoxDriver(firefoxOptions);
这行抛出错误

driver = new FirefoxDriver(firefoxOptions);
堆栈跟踪:

java.net.ConnectException: Failed to connect to localhost/127.0.0.1:5054 Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T14:04:26.12Z' System info: host: 'xxxxx-Lenovo-YOGA-3-Pro-1370', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-127-generic', java.version: '1.8.0_171' Driver info: driver.version: FirefoxDriver
org.openqa.selenium.WebDriverException
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T14:04:26.12Z'
System info: host: 'xxxxxx-Lenovo-YOGA-3-Pro-1370', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-127-generic', java.version: '1.8.0_171'
Driver info: driver.version: FirefoxDriver
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:92)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:207)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:130)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:125)
    at 
java.net.ConnectException:未能连接到本地主机/127.0.0.1:5054生成信息:版本:“3.12.0”,版本:“7c6e0b3”,时间:“2018-05-08T14:04:26.12Z”系统信息:主机:“xxxxx-Lenovo-YOGA-3-Pro-1370”,ip:“127.0.1.1”,os.name:“Linux”,os.arch:“amd64”,os.version:“4.0-127-generic”,java.version:'1.8.0_171'驱动程序信息:Driver.version:FirefoxDriver
org.openqa.selenium.WebDriverException
构建信息:版本:“3.12.0”,修订版:“7c6e0b3”,时间:“2018-05-08T14:04:26.12Z”
系统信息:主机:'xxxxxx-Lenovo-YOGA-3-Pro-1370',ip:'127.0.1.1',os.name:'Linux',os.arch:'amd64',os.version:'4.4.0-127-generic',java.version:'1.8.0_171'
驱动程序信息:Driver.version:FirefoxDriver
位于org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:92)
位于org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)
位于org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:207)
位于org.openqa.selenium.remote.RemoteWebDriver。(RemoteWebDriver.java:130)
位于org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:125)
在

不确定pluginFig文件的内容。通常通过Selenium 3.12.0、GeckoDriver v 0.20.1以编程方式在无头模式下调用Firefox浏览器,Firefox Quantum v60.0.2和Java您需要通过FirefoxOptions的实例传递
参数--headless,您可以使用以下解决方案:

  • 代码块:

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.firefox.FirefoxOptions;
    
    public class A_Firefox_Headless
    {
        public static void main(String[] args) 
        {
            System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver");
            FirefoxOptions options = new FirefoxOptions();
            options.addArguments("--headless");
            WebDriver driver =  new FirefoxDriver(options);
            System.out.println("Mozilla Firefox Headless Browser Initialized");
            driver.get("http://www.google.com");
            System.out.println("Page Title is : "+driver.getTitle());
            driver.quit();
        }
    }
    
  • 控制台输出:

    1528817592552   geckodriver INFO    geckodriver 0.20.1
    1528817592563   geckodriver INFO    Listening on 127.0.0.1:23550
    1528817593461   mozrunner::runner   INFO    Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "--headless" "-profile" "C:\\Users\\ATECHM~1\\AppData\\Local\\Temp\\rust_mozprofile.M2jv24VJ0QMg"
    *** You are running in headless mode.
    1528817597139   Marionette  INFO    Listening on port 8949
    1528817597577   Marionette  WARN    TLS certificate errors will be ignored for this session
    Jun 12, 2018 9:03:17 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: W3C
    Mozilla Firefox Headless Browser Initialized
    Page Title is : Google
    

    • 解决了这个问题。问题是,我使用的是一个损坏的geckodriver副本,它在stacktrace上方给出了以下错误

      --- exec-maven-plugin:1.2.1:exec (default-cli) @ fetcher-firefox ---
      path/to/geckodriver: 2: path/to/geckodriver: Syntax error: newline unexpected
      Exception in thread "main" org.openqa.selenium.WebDriverException: java.net.ConnectException: Failed to connect to localhost/127.0.0.1:15615
      
      构建信息:版本:“3.12.0”,修订版:“7c6e0b3”,时间:“2018-05-08T14:04:26.12Z” 系统信息:主机:'xxxxx-Lenovo-YOGA-3-Pro-1370',ip:'127.0.1.1',os.name:'Linux',os.arch:'amd64',os.version:'4.4.0-127-generic',java.version:'1.8.0_171'

      有关该问题的详细信息,请参见:

      System.setProperty(“webdriver.gecko.driver”,“/path/to/geckodriver”);firefoxOptions=新的firefoxOptions();firefoxOptions.addArguments(“--headless”);驱动程序=新的FirefoxDriver(firefoxOptions);错误仍然是相同的,与我们的环境不同的是操作系统。我使用的是Ubuntu(Linux),你的例子是Windows。另外,您使用的是什么版本的Selenium?您使用的是Java,您还没有声明firefoxOptions和驱动程序是什么。您需要将firefoxOptions声明为firefoxOptions的实例,将driver声明为WebDriver的实例,并且firefoxOptions在代码段之外声明为firefoxOptions。但是,在代码段之外,驱动程序被声明为FirefoxDriver而不是WebDriver。我做了更改,但仍然收到相同的错误。setProperty(“webdriver.gecko.driver”,“/path/to/geckodriver”);FirefoxOptions FirefoxOptions=新的FirefoxOptions();firefoxOptions.addArguments(“--headless”);WebDriver驱动程序=新的FirefoxDriver(firefoxOptions);您需要用geckodriverOS的绝对路径替换
      “/path/to/geckodriverOS”
      :Ubuntu(Linux)16.04 Selenium版本:“3.12.0”