2016-12-02 128 views
0

嘿家伙我试图运行使用Java库的Selenium示例代码。代码是来自网站的示例代码,但运行时收到错误。我不确定我应该做什么,因为错误不是特定的。我使用selenium 3 java libs运行OS X 10.11.6。我正在使用Eclipse。 Safari目前版本为10.01,启用了Safari浏览器开发菜单中的“允许远程自动化”。 代码:Selenium Webdriver错误与Java

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. 
 
    \t 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(); 
 
    } 
 
}

错误消息:

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 
 
\t at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:91) 
 
\t at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601) 
 
\t at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:241) 
 
\t at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:128) 
 
\t at org.openqa.selenium.safari.SafariDriver.<init>(SafariDriver.java:62) 
 
\t at org.openqa.selenium.safari.SafariDriver.<init>(SafariDriver.java:42) 
 
\t 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) 
 
\t at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:158) 
 
\t at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353) 
 
\t at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380) 
 
\t at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236) 
 
\t at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184) 
 
\t at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88) 
 
\t at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) 
 
\t at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) 
 
\t at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71) 
 
\t at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55) 
 
\t at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:160) 
 
\t at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:88) 
 
\t at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:108) 
 
\t at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:64) 
 
\t at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:141) 
 
\t at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82) 
 
\t ... 6 more 
 
Caused by: java.net.ConnectException: Connection refused (Connection refused) 
 
\t at java.net.PlainSocketImpl.socketConnect(Native Method) 
 
\t at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) 
 
\t at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) 
 
\t at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) 
 
\t at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) 
 
\t at java.net.Socket.connect(Socket.java:589) 
 
\t at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:74) 
 
\t at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:141) 
 
\t ... 21 more

+0

参考http://stackoverflow.com/questions/6876266/java-net-connectexception-connection-refused –

+0

好吗谢谢。我在公司代理,所以我假设它可能和防火墙配置 – Potion

+0

更新:刚刚回家,试了一下,这不是问题。我在那篇文章中看过一些我可能错过的东西吗? – Potion

回答

0

我不认为该端口被阻塞,这是因为底层的硒服务器不在该端口上运行。

当safaridriver进程没有从前一次运行中死亡时,我遇到了同样的“ConnectionException连接被拒绝”。在后续运行中,由于现有流程,示例的SafariDriver无法启动底层Selenium服务器。驱动器印刷以下错误

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

扔“ConnectionException连接被拒绝”,因为它无法连接到使用不同的端口从现有的过程中,新的Selenium服务器。

我认为一次只允许一个SafariDriver实例,并且每个实例都使用一个随机端口。因此,该示例实例化SafariDriver,但底层Selenium服务器无法启动,因为实例已在运行。然后驱动程序尝试使用它期望的端口(与现有实例不同)连接到新实例,并引发ConnectionException。

+0

这可能会回答别人的问题,但它并没有真正回答这个问题。 –

+0

@JoeC补充说明。 – rwakida

+0

这次我在笔记本电脑上重现了同样的错误。我检查了硒的哪个端口正在运行,看起来它没有完全启动。 – Potion

0

“无法启动服务器:另一safaridriver已经运行(@本地:35040)”

杀Safaridriver如果已经running..follow以下步骤:

  1. 打开终端和type命令 launchctl list | grep webdriverd
  2. 我们将得到运行端口和杀端口提命令 sudo kill *****

现在,它的做工精细