我有一个问题,因为我想在执行JavaScript时处理警报。全部包含在硒3.0.1,JDK 1.8和Firefox 51.0.1版本中。

我的代码:

public class SeleniumDemo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        System.setProperty("webdriver.gecko.driver", "C:\\Users\\Damian6666\\Downloads\\geckodriver\\geckodriver.exe");
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities.setCapability("marionette", true);

        WebDriver driver = new FirefoxDriver();
        driver.get("http://wikipedia.org");
        JavascriptExecutor jsx = (JavascriptExecutor) driver;
        jsx.executeScript("alert('Test Alerts');");

        try {
            Thread.sleep(3000);
            Alert alert = driver.switchTo().alert();
            alert.accept();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}


在输出中,我得到:

INFO: Detected dialect: W3C Exception in thread "main" org.openqa.selenium.WebDriverException: Failed to find value field Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:48:19 -0700' System info: host: 'Damian', ip: 'xxx.xxx.x.xxx', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_111' Driver info: org.openqa.selenium.firefox.FirefoxDriver Capabilities [{rotatable=false, raisesAccessibilityExceptions=false, marionette=true, firefoxOptions={args=[], prefs={}}, appBuildId=20170125094131, version=, platform=XP, proxy={}, command_id=1, specificationLevel=0, acceptSslCerts=false, processId=6204, browserVersion=51.0.1, platformVersion=6.1, XULappId={ec8030f7-c20a-464f-9b0e-13a3a9e97384}, browserName=firefox, takesScreenshot=true, takesElementScreenshot=true, platformName=windows_nt}] Session ID: 912b309c-46a1-4b15-9f9e-f411a6905e87 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.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:127)  at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:93) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:42) at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:537)
at selenium_demo.SeleniumDemo.main(SeleniumDemo.java:35) Picked up _JAVA_OPTIONS: -Xmx1024m Java Result: 1 BUILD SUCCESSFUL (total time: 20 seconds)##

最佳答案

您应该通过以下方式正确实例化驱动程序:

WebDriver driver = new FirefoxDriver(capabilities);


您好像遇到了以下错误:https://github.com/mozilla/geckodriver/issues/431

07-26 03:47