有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

selenium无法使用PhantomJS Webdriver和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");
    }

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

如果您有任何可能的解决方案或想法可以寻求帮助,我们将不胜感激。谢谢


共 (1) 个答案

  1. # 1 楼答案

    答案其实很简单。。。在这让我疯狂了几天之后,我想,为什么不更新Ghostdriver。。。所以我做到了,现在一切都很顺利。也许是我应该尝试的第一件事,但是嘿嘿

    问题在于Phantomjs 1.9.7。问题在更新到1.9.8版后得到修复