无法通过登录页面

Cannot get Past the Login Page

我无法通过登录页面。我正确地抓取了输入元素,填充它们并提交,但我仍然在原始页面上结束。我不确定确切的问题出在哪里,我尝试了“.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" while the next page upon successfully logging in would be "https://epicmafia.com/lobby”。

编辑:供参考:第三个子元素是前两个(用户名和密码)之后的实际 "Login" 按钮。

应该在 <form> 元素上执行 submit()

WebElement parent = driver.findElement(By.id("login_form"));
// fill the fields here
parent.submit();

您可以试试这个代码:

由于我没有凭据,点击登录按钮后会显示错误,您可以在相应的sendKeys("")中提供用户名和密码命令)

public class FFFF { 

    static WebDriver driver; 
    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "D:\Automation\chromedriver.exe");
        driver  = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://epicmafia.com/home");
        driver.findElement(By.xpath("//form[@id='login_form']/descendant::input[@placeholder='username']")).sendKeys("username");
        driver.findElement(By.xpath("//form[@id='login_form']/descendant::input[@placeholder='password']")).sendKeys("password");
        driver.findElement(By.xpath("//form[@id='login_form']/descendant::input[@name='submit']")).click();

    }
}

要将字符序列发送到用户名密码字段你需要诱导WebDriverWaitclick()Login 按钮上你可以使用以下解决方案:

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    # other lines of code
    driver.get("https://epicmafia.com/home")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='user[username]']"))).send_keys("FF")
    driver.find_element_by_css_selector("input[name='user[password]']").send_keys("FF")
    driver.find_element_by_css_selector("input.red[value='Login']").click()
    
  • 浏览器快照:

在查看回复并稍微弄乱之后,答案不是抓取元素、填充元素甚至提交时的错误。问题来自超时不起作用,我大概认为是这样。

我将驱动程序移动到它自己的方法终止开关,这样初始调用在被手动终止之前有无限期的加载时间,而不是像我最初发布的代码那样自动终止。

现在我的登录问题已解决,我必须调查为什么超时不起作用。谢谢大家的帮助。