简体   繁体   English

硒连接拒绝错误

[英]Selenium connection refused error

I am trying to setup a simple Selenium project up but I can't get the initial setup right. 我正在尝试建立一个简单的Selenium项目,但无法正确进行初始设置。 My current code is: 我当前的代码是:

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

public class webdriverdemo {

    public static void main(String[] args) {

        System.setProperty("webdriver.gecko.driver", "geckodriver");
        FirefoxOptions firefoxOptions = new FirefoxOptions();
        firefoxOptions.setCapability("marionette", true);
        WebDriver driver = new FirefoxDriver(firefoxOptions);
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        driver.navigate().to("http://www.calculator.net/");

        driver.manage().window().maximize();

        driver.findElement(By.xpath(".//*[@id = 'menu']/div[3]/a")).click();

        driver.findElement(By.xpath(".//*[@id = 'menu']/div[4]/div[3]/a")).click();

        driver.findElement(By.id("cpar1")).sendKeys("10");

        driver.findElement(By.id("cpar2")).sendKeys("50");

        driver.findElement(By.xpath(".//*[@id = 'content']/table/tbody/tr[2]/td/input[2]")).click();

        String result = driver.findElement(By.xpath(".//*[@id = 'content']/p[2]/font/b")).getText();

        System.out.println("The result is " + result);

        driver.close();

    }

}

And the error I'm getting is: 我得到的错误是:

1510139780110   geckodriver INFO    geckodriver 0.19.1
1510139780119   geckodriver INFO    Listening on 127.0.0.1:3448
1510139781051   mozrunner::runner   INFO    Running command: "/usr/lib64/firefox/firefox" "-marionette" "-profile" "/tmp/rust_mozprofile.KnF05xRcbAZX"
1510139784365   Marionette  INFO    Listening on port 2828
kf5.kio.core: Refilling KProtocolInfoFactory cache in the hope to find "ircs"
kf5.kio.core: Refilling KProtocolInfoFactory cache in the hope to find "mailto"
kf5.kio.core: Refilling KProtocolInfoFactory cache in the hope to find "irc"
Exception in thread "main" org.openqa.selenium.WebDriverException: connection refused
Build info: version: '3.7.1', revision: '8a0099a', time: '2017-11-06T21:01:39.354Z'
System info: host: 'linux-m2zi.suse', ip: 'Unknown', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.92-31-default', java.version: '1.8.0_144'
Driver info: driver.version: FirefoxDriver
remote stacktrace: stack backtrace:
   0:           0x4edb3c - backtrace::backtrace::trace::hc4bd56a2f176de7e
   1:           0x4edb72 - backtrace::capture::Backtrace::new::he3b2a15d39027c46
   2:           0x440ac8 - webdriver::error::WebDriverError::new::ha0fbd6d1a1131b43
   3:           0x447ba9 - geckodriver::marionette::MarionetteHandler::create_connection::hf0532ddb9e159684
   4:           0x428570 - <webdriver::server::Dispatcher<T, U>>::run::h2119c674d7b88193
   5:           0x4029b9 - std::sys_common::backtrace::__rust_begin_short_backtrace::h21d98a9ff86d4c25
   6:           0x40be65 - std::panicking::try::do_call::h5cff0c9b18cfdbba
   7:           0x5e6a6c - panic_unwind::__rust_maybe_catch_panic
                        at /checkout/src/libpanic_unwind/lib.rs:99
   8:           0x41eb22 - <F as alloc::boxed::FnBox<A>>::call_box::h413eb1d9d9f1c473
   9:           0x5df13b - alloc::boxed::{{impl}}::call_once<(),()>
                        at /checkout/src/liballoc/boxed.rs:692
                         - std::sys_common::thread::start_thread
                        at /checkout/src/libstd/sys_common/thread.rs:21
                         - std::sys::imp::thread::{{impl}}::new::thread_start
                        at /checkout/src/libstd/sys/unix/thread.rs:84
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$new$0(W3CHandshakeResponse.java:57)
    at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$getResponseFunction$2(W3CHandshakeResponse.java:104)
    at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$0(ProtocolHandshake.java:123)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:958)
    at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
    at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:498)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:152)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:464)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:126)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:73)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:142)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:600)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:219)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:142)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:120)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:98)
    at com.seltri.webdriverdemo.main(webdriverdemo.java:14)

I have tried a couple of things but I'm pretty new to this stuff so I wasn't really able to solve it. 我已经尝试了几件事,但是对于这些东西我还是很陌生,所以我真的无法解决它。 What can I do to fix this? 我该怎么做才能解决此问题? I've also tried using RemoteWebDriver but that also didn't work. 我也尝试过使用RemoteWebDriver,但这也没用。 I haven't tested with chrome etc. 我没有用铬等测试过。

I am using OpenSUSE Linux with Firefox 52.4 我在Firefox 52.4中使用OpenSUSE Linux

You need to make a couple of changes here: 您需要在此处进行一些更改:

  1. Instead of import org.openqa.selenium.*; 而不是import org.openqa.selenium.*; we need to be specific as import org.openqa.selenium.WebDriver; 我们需要特定为import org.openqa.selenium.WebDriver;
  2. You need to provide the absolute path of the geckodriver as below: 您需要提供geckodriver的绝对路径,如下所示:

     System.setProperty("webdriver.gecko.driver", "/usr/bin/geckodriver"); 

Ensure that geckodriver binary is copied within /usr/bin/ directory with required access. 确保具有所需访问权限的geckodriver二进制文件已复制到/usr/bin/目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM