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

时间:2015-01-22 17:35:36

标签: java selenium webdriver phantomjs

我正在用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");
    }

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

非常感谢任何寻求帮助的解决方案或想法。感谢

1 个答案:

答案 0 :(得分:1)

实际上答案非常简单......在此之后让我疯狂了几天,我为什么不更新Ghostdriver ......所以我做了,现在一切都运转良好。可能是我应该尝试的第一件事,但是嘿嘿......

问题在于Phantomjs 1.9.7。更新到版本1.9.8后,问题已得到解决。