0

Hey guys Im trying to run an example code of Selenium using the Java libs. The code is the sample code from the website but I receive an error on run. I am not sure what I am supposed to do as the error isn't specific. I am running OS X 10.11.6 with selenium 3 java libs. I am using Eclipse. Safari is currently version 10.01 with "Allow Remote Automation" from the safari develop menu enabled. Code:

package seleniumPackage;

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.safari.SafariDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

//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.ExpectedCondition;
//import org.openqa.selenium.support.ui.WebDriverWait;

public class Selenium2Example  {
    public static void main(String[] args) {
        // Create a new instance of the Firefox driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
     WebDriver driver = new SafariDriver();

        // And now use this to visit Google
        driver.get("http://www.google.com");
        // Alternatively the same thing can be done like this
        // driver.navigate().to("http://www.google.com");

        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("q"));

        // Enter something to search for
        element.sendKeys("Cheese!");

        // Now submit the form. WebDriver will find the form for us from the element
        element.submit();

        // Check the title of the page
        System.out.println("Page title is: " + driver.getTitle());
        
        // Google's search is rendered dynamically with JavaScript.
        // Wait for the page to load, timeout after 10 seconds
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                return d.getTitle().toLowerCase().startsWith("cheese!");
            }
        });

        // Should see: "cheese! - Google Search"
        System.out.println("Page title is: " + driver.getTitle());
        
        //Close the browser
        driver.quit();
    }
}

Error Message:

Dec 01, 2016 11:32:01 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
Exception in thread "main" org.openqa.selenium.WebDriverException: org.apache.http.conn.HttpHostConnectException: Connect to localhost:48334 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused)
Build info: version: 'unknown', revision: '1969d75', time: '2016-10-18 09:43:45 -0700'
System info: host: 'Adrians-MacBook-Pro.local', ip: '192.168.68.205', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.11.6', java.version: '1.8.0_111'
Driver info: driver.version: SafariDriver
 at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:91)
 at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
 at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:241)
 at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:128)
 at org.openqa.selenium.safari.SafariDriver.<init>(SafariDriver.java:62)
 at org.openqa.selenium.safari.SafariDriver.<init>(SafariDriver.java:42)
 at seleniumPackage.Selenium2Example.main(Selenium2Example.java:24)
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to localhost:48334 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused)
 at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:158)
 at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
 at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
 at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
 at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
 at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
 at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
 at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
 at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71)
 at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
 at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:160)
 at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:88)
 at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:108)
 at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:64)
 at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:141)
 at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
 ... 6 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:74)
 at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:141)
 ... 21 more
Potion
  • 785
  • 1
  • 14
  • 36
  • Refer http://stackoverflow.com/questions/6876266/java-net-connectexception-connection-refused – Sanjay Bhimani Dec 02 '16 at 07:51
  • okay thank you. I'm on a company proxy so I'm assuming it maybe that and the firewall config – Potion Dec 02 '16 at 10:08
  • update: just got home and tried it out that wasn't the issue. did i glance something in that post that i may of missed? – Potion Dec 02 '16 at 18:24
  • Something is blocking access to port 48334. Have you [configured appache](https://www.ntu.edu.sg/home/ehchua/programming/howto/Apache_HowToConfigure.html) to specifically allow that port? – MikeJRamsey56 Dec 04 '16 at 15:37
  • @MikeJRamsey56 No. While I do have an Apache setup locally setup with MAMP for a local test server, isnt running in the background when programming a selenium test project. So I wouldn't need to configure it in this case would i? – Potion Dec 04 '16 at 20:27

2 Answers2

0

I don't think that the port is blocked, it's because the underlying Selenium server is not running on that port.

I ran into the same "ConnectionException connection refused", when the safaridriver process was not killed from a previous run. On subsequent runs, the example's SafariDriver could not start the underlying Selenium server due to the existing process. The driver printed the following error

"Unable to start the server: another safaridriver is already running (@ localhost:35040)"

and threw "ConnectionException Connection refused" because it could not connect to the new Selenium server which used a different port from the existing process.

I think only one instance of the SafariDriver is allowed at a time, and each instance uses a random port. Thus the example instantiates the SafariDriver, but the underlying Selenium server can't start because an instance is already running. The driver then tries to connect to the new instance using the port it expects (which is different than the existing instance), and throws a ConnectionException.

rwakida
  • 26
  • 2
  • This might answer someone else's question, but it doesn't really answer this one. – Joe C Dec 05 '16 at 21:38
  • @JoeC added clarification. – rwakida Dec 05 '16 at 22:45
  • I am reproducing the same error on my laptop this time around. I checked which ports selenium is running and it looks like it failed to start completely. – Potion Dec 07 '16 at 22:11
  • Hello guys just wanted to chime in in case any other users had this issue and still could not figure it out. You need to update your mac machines to Sierra as well. I upgraded from El Capitan to Sierra and now I can run my tests without any issues. – Potion Dec 08 '16 at 01:25
0

"Unable to start the server: another safaridriver is already running (@ localhost:35040)"

Kill the Safaridriver if already running..follow below steps:

  1. Open terminal and type command launchctl list | grep webdriverd
  2. We will get running port and kill the port as mention command sudo kill *****

Now its working Fine

Jayden Meyer
  • 703
  • 2
  • 18
  • 28
Nagu
  • 3
  • 3