Firefox浏览器没有打开指定的URL而关闭

时间:2018-11-07 12:11:57

标签: selenium

我是Selenium的新手,从我在标识元素时指定绝对路径可以看出。我写了一个简单的代码来打开gmail登录页面 `

package SeleniumScripts;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class LogintoGmail {

    public static void main(String[] args) {

        System.setProperty("webdriver.gecko.driver", "D:\\Selenium\\geckodriver-v0.19.1-win64\\geckodriver.exe");`enter code here`
    //  System.setProperty("webdriver.ie.driver", "D:\\Selenium\\IEDriverServer_x64_3.8.0\\IEDriverServer.exe");
        WebDriver driver=new FirefoxDriver();
    //  WebDriver driver=new InternetExplorerDriver();  
        driver.get("http://www.gmail.com/");
        driver.manage().window().maximize();
        driver.findElement(By.xpath("/html/body/div[1]/div/div[2]/div/div[2]/form/div[2]/div/div/div/ul[1]/li[1]/div/div[2]/p[2]")).click();
        driver.findElement(By.xpath("/html/body/div[1]/div/div[2]/div/div[2]/div/div/div[2]/div/div[1]/div/form/content/section/div/content/div[1]/div/div[1]/div/div[1]/input")).sendKeys("12!harley");
        String apptitle=driver.getTitle();
        System.out.println(apptitle);

    }

}

` 运行代码时,Firefox打开,然后打开URL,然后关闭。我在执行过程中遇到以下错误:

  

1541589284453   Marionette  DEBUG   Remote service is active
1541589284765   Marionette  DEBUG   Accepted connection 0 from 127.0.0.1:51510
1541589284777   Marionette  TRACE   0 -> [0,1,"newSession",{"acceptInsecureCerts":true,"browserName":"firefox","capabilities":{"desiredCapabilities":{"acceptInsecureCerts":true,"browserName":"firefox"}}}]
1541589284778   Marionette  TRACE   0 <- [1,1,{"error":"unknown command","message":"newSession","stacktrace":"WebDriverError@chrome://marionette/content/error.js:178: ... et@chrome://marionette/content/server.js:245:8\n_onJSONObjectReady/<@chrome://marionette/content/transport.js:490:9\n"},null]
Nov 07, 2018 4:44:45 PM org.openqa.selenium.remote.ErrorCodes toStatus
INFO: HTTP Status: '404' -> incorrect JSON status mapping for 'unknown error' (500 expected)
Exception in thread "main" org.openqa.selenium.WebDriverException: newSession
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T15:15:03.216Z'
System info: host: 'USER-PC', ip: '192.168.43.64', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '9.0.4'
Driver info: driver.version: FirefoxDriver
remote stacktrace: WebDriverError@chrome://marionette/content/error.js:178:5
UnknownCommandError@chrome://marionette/content/error.js:472:5
despatch@chrome://marionette/content/server.js:290:13
execute@chrome://marionette/content/server.js:271:11
onPacket/<@chrome://marionette/content/server.js:246:15
onPacket@chrome://marionette/content/server.js:245:8
_onJSONObjectReady/<@chrome://marionette/content/transport.js:490:9

    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.base/java.lang.reflect.Constructor.newInstance(Unknown Source)
    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.base/java.util.stream.ReferencePipeline$3$1.accept(Unknown Source)
    at java.base/java.util.Spliterators$ArraySpliterator.tryAdvance(Unknown Source)
    at java.base/java.util.stream.ReferencePipeline.forEachWithCancel(Unknown Source)
    at java.base/java.util.stream.AbstractPipeline.copyIntoWithCancel(Unknown Source)
    at java.base/java.util.stream.AbstractPipeline.copyInto(Unknown Source)
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
    at java.base/java.util.stream.FindOps$FindOp.evaluateSequential(Unknown Source)
    at java.base/java.util.stream.AbstractPipeline.evaluate(Unknown Source)
    at java.base/java.util.stream.ReferencePipeline.findFirst(Unknown Source)
    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:136)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:207)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:130)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:125)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:103)
    at SeleniumScripts.LogintoGmail.main(LogintoGmail.java:12)

请注意,一周前运行良好,但现在出现错误。

1 个答案:

答案 0 :(得分:0)

    package SeleniumScripts;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class LogintoGmail {
    public static void main(String[] args) {

        System.setProperty("webdriver.gecko.driver", "D:\\Selenium\\geckodriver-v0.19.1-win64\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();


        driver.get("http://www.gmail.com/");
        driver.manage().window().maximize();
        driver.findElement(By.xpath("X/PATH")).click();
        driver.findElement(By.xpath("X/PATH")).sendKeys("12!harley");
        String apptitle = driver.getTitle();
        System.out.println(apptitle);

尝试复制我所做的事情,该代码虽然非常混乱,但只是正确,只是在声明firefox驱动程序时没有空格,在两个实例中您没有适当的间距,并且确实使差异。

当代码错误时,很多时候驱动程序会强制浏览器打开,但会保留URL为空或null值,而不会继续测试。

如果我是你,我将首先打开页面并在脚本之上构建。