我无法通过登录页面。我正确地获取了输入元素,填充了它们,并提交了它们,但我仍然停留在原始页面上。我不确定确切的问题在哪里,我尝试了".submit,.click,并模拟了一个javascript ENTER来提交凭据。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//Create driver, javascript enabled
WebDriver driver = new HtmlUnitDriver(true);
driver.get("https://epicmafia.com/home");
//Get parent of login form
WebElement parent = driver.findElement(By.id("login_form"));
//Get both inputs of the login form
//First is name
//Second is password
ArrayList<WebElement> children = new ArrayList<WebElement>();
for(WebElement input : parent.findElements(By.cssSelector("input")))
children.add(input);
//Fill in name
children.get(0).sendKeys("USERNAME");
//Fill in password
children.get(1).sendKeys("PASSWORD");
//Wait for good measure
driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
//Submit credentials
children.get(1).submit();
//Double check inputs are desired values
System.out.println("The username is: " + children.get(0).getAttribute("value"));
System.out.println("The password is: " + children.get(1).getAttribute("value"));
//Check if pass login page
System.out.println("End URL is: " + driver.getCurrentUrl());
driver.quit();
}
登录页面是"https://epicmafia.com/home“,而成功登录后的下一个页面将是"https://epicmafia.com/lobby”。
编辑:供参考:第三个子元素是前两个元素(用户名和密码)后面的实际"Login“按钮。
发布于 2018-06-18 02:45:31
在查看了回复并花了一些时间后,答案并不是在抓取元素、填充元素甚至提交元素时出错。问题来自于超时不起作用,而我大概是这么认为的。
我将驱动程序移到它自己的方法kill switch,这样初始调用在手动终止之前有无限的时间加载,而不是像我最初发布的代码那样自动终止。
现在我的登录问题已经解决了,我必须检查为什么超时不起作用。谢谢大家的帮助。
发布于 2018-06-17 17:51:45
应该在<form>
元素上执行submit()
WebElement parent = driver.findElement(By.id("login_form"));
// fill the fields here
parent.submit();
https://stackoverflow.com/questions/50895379
复制相似问题