2

I must not be setting the System properties correctly. Otherwise I can't at all understand why the exception below is cannot connect to localhost. Or, might this be some sort of networking or other problem?

crash:

thufir@doge:~/NetBeansProjects/selenium$ 
thufir@doge:~/NetBeansProjects/selenium$ gradle clean fatJar;java -jar build/libs/selenium-all.jar 

BUILD SUCCESSFUL in 50s
4 actionable tasks: 4 executed
Jul 09, 2017 12:48:57 PM net.bounceme.dur.web.selenium.Main main
INFO: init..
Exception in thread "main" org.openqa.selenium.WebDriverException: org.apache.http.conn.HttpHostConnectException: Connect to localhost:11551 [localhost/127.0.0.1] failed: Connection refused (Connection refused)
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: 'doge', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-21-generic', java.version: '1.8.0_111'
Driver info: driver.version: FirefoxDriver
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:91)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:250)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:236)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:137)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:191)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:108)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:104)
    at net.bounceme.dur.web.selenium.Scraper.scrape(Scraper.java:24)
    at net.bounceme.dur.web.selenium.Main.run(Main.java:20)
    at net.bounceme.dur.web.selenium.Main.main(Main.java:15)
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to localhost:11551 [localhost/127.0.0.1] failed: Connection refused (Connection refused)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:159)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:359)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
    at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:139)
    at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:87)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:343)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:159)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:142)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
    ... 10 more
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:75)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
    ... 25 more
thufir@doge:~/NetBeansProjects/selenium$ 

code:

package net.bounceme.dur.web.selenium;

import java.util.Properties;
import java.util.logging.Logger;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Scraper {

    private static final Logger log = Logger.getLogger(Scraper.class.getName());

    public Scraper() {
    }

    public void scrape(Properties p) {
        log.fine(p.toString());

        String key = "webdriver.gecko.driver";
        String url = p.getProperty("url");
        String value = p.getProperty(key);
        System.setProperty(key, value);
        // System.setProperties(p);

        log.info(url);
        log.info(value);

        WebDriver driver = new FirefoxDriver();
        driver.get(url);
    }

}

properties file:

url:http://www.google.com
url2:file:///home/thufir/wget/foo.html
webdriver.gecko.driver:/usr/bin/firefox
1

The webdriver.gecko.driver property needs to be pointed to a geckodriver executable (reference), not firefox browser itself. Make sure geckodriver executable has "x" (execution) permissions.

You can also add it to PATH:

export PATH=$PATH:/path/to/geckodriver

and then just start the driver without setting the system properties:

WebDriver driver = new FirefoxDriver();
0

The official documentation mozilla/geckodriver states:

Selenium client bindings will pick up the geckodriver binary executable from your system’s PATH environmental variable unless you override it by setting the webdriver.gecko.driver Java VM system property:

System.setProperty("webdriver.gecko.driver", "/home/user/bin");

Or by passing it as a flag to the java(1) launcher:

% java -Dwebdriver.gecko.driver=/home/user/bin YourApplication

So you can install gecko-driver and export it as environment path. Then, Selenium is able to find the driver itself. In Mac OS, you can install gecko-driver using brew, and the command is exported automatically:

$ brew install geckodriver
$ type geckodriver
geckodriver is /usr/local/bin/geckodriver

Now, in the Java code, there's no need to override the gecko-driver path:

WebDriver driver = new FirefoxDriver();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.