我刚刚开始在我的办公室设置自动化 硒的帮助,但只在初始水平打击。我用了一个 基本代码最初只是为了检查浏览器是否正在工作和处理 代码进一步但有问题。
import java.io.IOException;
import java.net.InetAddress;
import java.util.concurrent.TimeUnit;
import org.apache.http.conn.HttpHostConnectException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
public class ABC {
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver","C:\\Users\\12345678\\
AppData\\Local\\Mozilla Firefox\\firefox.exe");
DesiredCapabilities dc=DesiredCapabilities.firefox();
FirefoxProfile profile = new FirefoxProfile();
dc.setCapability(FirefoxDriver.PROFILE, profile);
WebDriver driver = new FirefoxDriver(dc);
driver.get("http://www.google.com");
driver.manage().timeouts().implicitlyWait(10L, TimeUnit.SECONDS);
}`
**Below is the error i am facing**
`
Exception in thread "main" org.openqa.selenium.WebDriverException:
org.apache.http.conn.HttpHostConnectException: Connect to
localhost:44853
[localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection
refused: connect
Build info: version: '3.3.1', revision: '5234b32', time: '2017-03-10
09:04:52 -0800'
System info: host: 'NODHCMSLTP1115', ip: '10.203.124.34', os.name:
'Windows
7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_92'
Driver info: driver.version: FirefoxDriver at
org.openqa.selenium.remote.service.DriverCommandExecutor.execute(Driver
CommandExecutor.java:91) at
org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.
java:604)at
org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver
.java:244)
at org.openqa.selenium.remote.RemoteWebDriver.<init>
(RemoteWebDriver.java:131)
at org.openqa.selenium.firefox.FirefoxDriver.<init>
(FirefoxDriver.java:218)
at org.openqa.selenium.firefox.FirefoxDriver.<init>
(FirefoxDriver.java:125)
at org.openqa.selenium.firefox.FirefoxDriver.<init>
(FirefoxDriver.java:150)
at seleniumProjectauto.Rks_Asu.main(Rks_Asu.java:27)
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to
localhost:44853 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed:
Connection refused: connect
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(InternalHttp
Client
.java:184)
at
org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttp
Client.java:71)
at
org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttp
Client.java:55)
at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute
(ApacheHttpClient.java:142)
at
org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttp
Client.java:88)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(Protocol
Handshake.java:296)
at
org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHand
shake.java:113)
at
org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommand
Executor.java:141)
at
org.openqa.selenium.remote.service.DriverCommandExecutor.execute
(DriverCommandExecutor.java:82)
... 7 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at
java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl
.java:85)
at
java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl
.java:350) at
java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocket
Impl.java:206)
at
java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.
java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
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)
... 22 more`
答案 0 :(得分:1)
您的代码看起来不错,但有几点:
使用Selenium 3.x&amp; Mozila Firefox 52.x您需要从this location下载最新的gecko驱动程序并保存。
您需要提供“geckodriver.exe”的绝对路径,而不是“firefox.exe”
替换:
System.setProperty("webdriver.gecko.driver","C:\\Users\\12345678\\
AppData\\Local\\Mozilla Firefox\\firefox.exe");
人:
System.setProperty("webdriver.gecko.driver","C:\\your_directory\\geckodriver.exe");
最后,您的代码将如下所示:
System.setProperty("webdriver.gecko.driver", "C:\\SeleniumUtilities\\BrowserDrivers\\geckodriver.exe");
DesiredCapabilities dc = DesiredCapabilities.firefox();
FirefoxProfile profile = new FirefoxProfile();
dc.setCapability(FirefoxDriver.PROFILE, profile);
WebDriver driver = new FirefoxDriver(dc);
driver.manage().window().maximize();
driver.get("http:\\gmail.com");
这段代码对我很好。
如果这有助于您,请告诉我。