我正在尝试设置一个简单的Selenium项目,但我无法正确设置初始设置。我目前的代码是:
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();
}
}
我得到的错误是:
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)
我尝试了很多东西,但我对这些东西很陌生,所以我真的无法解决它。我该怎么做才能解决这个问题?我也尝试过使用RemoteWebDriver,但这也没有用。我没有用铬等测试过。
我正在使用OpenSUSE Linux和Firefox 52.4
答案 0 :(得分:0)
您需要在此处进行一些更改:
import org.openqa.selenium.*;
import org.openqa.selenium.WebDriver;
您需要提供 geckodriver
的绝对路径,如下所示:
System.setProperty("webdriver.gecko.driver", "/usr/bin/geckodriver");
确保在
geckodriver
目录中复制/usr/bin/
二进制文件并进行必要的访问。