无法使用 PhantomJS Webdriver 和 Java 提交表单

Unable to submit forms using PhantomJS Webdriver and Java

我正在 java 中编写程序。其中一项功能是使用用户名和密码登录网站。我最初编写这个基本功能是为了使用 ChromeDriver 登录页面。

    public void Login() {
        System.out.println("Logging In");
        driver.get(url);
        ScreenShot("/Users/Adam/Documents/Screens/1.jpg");
        WebElement login = driver.findElement(By.xpath("//*[@id=\"username\"]"));
        login.sendKeys(username + Keys.TAB + password);
        ScreenShot("/Users/Adam/Documents/Screens/2.jpg");
    }

上面的函数在 Chromedriver 中有效,但是当我 运行 在 phantomjs 中使用相同的函数时,结果不一样。这是错误输出。

Exception in thread "main" org.openqa.selenium.WebDriverException: {"errorMessage":"'undefined' is not an object (evaluating 'c.name=this.name')","request":{"headers":{"Accept":"application/json, image/png","Connection":"Keep-Alive","Content-Length":"63","Content-Type":"application/json; charset=utf-8","Host":"localhost:48788"},"httpVersion":"1.1","method":"POST","post":"{\"id\":\":wdc:1421947793877\",\"value\":[\"usernamepassword\"]}","url":"/value","urlParsed":{"anchor":"","query":"","file":"value","directory":"/","path":"/value","relative":"/value","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/value","queryKey":{},"chunks":["value"]},"urlOriginal":"/session/4562eec0-a25c-11e4-a2af-d1362c6e21bf/element/%3Awdc%3A1421947793877/value"}} Command duration or timeout: 811 milliseconds

通过查看上面的错误,看起来信息正在提交到表单,但有些页面却抛出错误。

我也试过另一种方法,虽然还是不行,但也不报错...

    public void Login() {
        System.out.println("Logging In");
        driver.get(url);
        ScreenShot("/Users/Adam/Documents/Screens/1.jpg");
        WebElement login = driver.findElement(By.xpath("//*[@id=\"login\"]"));
        login.sendKeys(Keys.TAB + username + Keys.TAB + password + Keys.ENTER);
        ScreenShot("/Users/Adam/Documents/Screens/2.jpg");
    }

上面的函数没有查找用户名字段,而是查找表单本身并尝试输入数据。结果是填写了用户名和密码,但没有提交表单。

任何可能的解决方案或关于在哪里寻求帮助的想法将不胜感激。谢谢

实际上答案很简单...虽然这让我发疯了几天,但为什么不更新 Ghostdriver...所以我做了,现在一切正常。可能是我应该尝试的第一件事,但是嘿嘿...

问题出在 Phantomjs 1.9.7 上。更新到版本 1.9.8 后问题已解决。