嘿傢伙我試圖運行使用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
參考http://stackoverflow.com/questions/6876266/java-net-connectexception-connection-refused –
好嗎謝謝。我在公司代理,所以我假設它可能和防火牆配置 – Potion
更新:剛剛回家,試了一下,這不是問題。我在那篇文章中看過一些我可能錯過的東西嗎? – Potion