无法使用selenium web driver

时间:2017-10-21 01:19:10

标签: firefox selenium-webdriver

我无法使用selenium webdriver 3.4.0启动Firefox浏览器

浏览器版本:Firefox 54.0.1

这是我的代码:

public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");
    System.setProperty("webdriver.gecko.driver", "C:\\xx\\geckodriver.exe");
     WebDriver driver=new FirefoxDriver();
    driver.get("http://www.google.co.in");
    driver.quit();
 }
}

获取错误

  

1508548441651 geckodriver INFO geckodriver 0.18.0   1508548441660 geckodriver INFO收听127.0.0.1:48046异常   在线程“main”org.openqa.selenium.WebDriverException中:   org.apache.http.conn.HttpHostConnectException:连接到   localhost:48046 [localhost / 127.0.0.1,localhost / 0:0:0:0:0:0:0:1]   失败:连接被拒绝:连接构建信息:版本:'3.4.0',   版本:'未知',时间:'未知'系统信息:主持人:   'ADMINRG-627BJ6K',ip:'192.168.1.3',os.name:'Windows 10',os.arch:   'amd64',os.version:'10 .0',java.version:'1.8.0_131'驱动程序信息:   driver.version:FirefoxDriver at   org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:91)     在   org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)     在   org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:250)     在   org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:236)     在   org.openqa.selenium.remote.RemoteWebDriver。(RemoteWebDriver.java:137)     在   org.openqa.selenium.firefox.FirefoxDriver。(FirefoxDriver.java:191)     在   org.openqa.selenium.firefox.FirefoxDriver。(FirefoxDriver.java:108)     在   org.openqa.selenium.firefox.FirefoxDriver。(FirefoxDriver.java:104)     在com.webdriver.Testing.main(Testing.java:15)引起:   org.apache.http.conn.HttpHostConnectException:连接到   localhost:48046 [localhost / 127.0.0.1,localhost / 0:0:0:0:0:0:0:1]   失败:拒绝连接:连接到   org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:159)     在   org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:359)     在   org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381)     在   org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)     在   org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)     在   org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)     在   org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)     在   org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)     在   org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)     在   org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)     在   org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:139)     在   org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:87)     在   org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:343)     在   org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:159)     在   org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:142)     在   org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)     ... 8更多引起:java.net.ConnectException:拒绝连接:   在java.net.DualStackPlainSocketImpl.waitForConnect连接(Native   方法)在java.net.DualStackPlainSocketImpl.socketConnect(未知   来自java.net.AbstractPlainSocketImpl.doConnect(未知来源)     at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)     在java.net.AbstractPlainSocketImpl.connect(未知来源)at   java.net.PlainSocketImpl.connect(未知来源)at   java.net.SocksSocketImpl.connect(未知来源)at   java.net.Socket.connect(未知来源)at   org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:75)     在   org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)   ......还有23个

3 个答案:

答案 0 :(得分:2)

您可以在maven中使用“ Webdrivermanager”插件来代替特定浏览器的驱动程序,因此它将解决大多数与浏览器及其实例有关的问题。即使这样,也对我克服浏览器相关问题很有帮助。例如。 1.使用Maven添加Webdrivermanager插件 2.直接调用您选择的浏览器,例如firefox,如下所述。

FirefoxDriverManager.getInstance()。setup(); driver = new FirefoxDriver();

答案 1 :(得分:2)

在某些版本的Firefox浏览器中,驱动程序无法使用属性“ webdriver.gecko.driver”启动浏览器。替代此方法,您可以尝试以下代码:

 DesiredCapabilities capabilities = DesiredCapabilities.firefox();
    capabilities.setCapability("marionette", true);
    WebDriver driver = new FirefoxDriver(capabilities);

答案 2 :(得分:0)

尝试这种方式:我使用的是C#,但这种方式对我有用。您可以按照Java中的相同方式执行操作 提供'firefox.exe'和'GeckoDriver'作为您的本地路径

var binary = new FirefoxBinary(@"----Firefox.exe Path------");
var profile = new FirefoxProfile();
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(@"----GeckoDriver Path------");
service.FirefoxBinaryPath = @"----Firefox.exe Path------";
driverInstance = new FirefoxDriver(service);