Selenium测试脚本通过新的ajax登录表单登录谷歌帐户

时间:2017-08-30 06:04:55

标签: java selenium selenium-webdriver

我可以编写脚本将我的电子邮件地址放入电子邮件元素中。但是,一旦点击下一步脚本,谷歌就会使用ajax动态地将该电子邮件元素替换为密码元素。这是我被困住的地方,无法在该元素中提供密码而无法登录。

网址:https://accounts.google.com/signin/v2/identifier?flowName=GlifWebSignIn&flowEntry=ServiceLogin

请编写selenium测试脚本来实现此目的。

3 个答案:

答案 0 :(得分:4)

以下是使用您的有效凭据访问网址https://accounts.google.com/signin登录并在您的控制台上打印Page Title的代码块:

String url = "https://accounts.google.com/signin";
driver.get(url);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
WebElement email_phone = driver.findElement(By.xpath("//input[@id='identifierId']"));
email_phone.sendKeys("your_email");
driver.findElement(By.id("identifierNext")).click();
WebElement password = driver.findElement(By.xpath("//input[@name='password']"));
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(password));
password.sendKeys("your_password");
driver.findElement(By.id("passwordNext")).click(); 
System.out.println(driver.getTitle());
driver.quit();

控制台输出:

Google Accounts

答案 1 :(得分:0)

下面的代码适用于我

     driver.get("https://accounts.google.com/signin/v2/identifier?flowName=GlifWebSignIn&flowEntry=ServiceLogin");
     driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
     driver.findElement(By.name("identifier")).sendKeys("xyz@gmail.com");
     driver.findElement(By.xpath("//span[contains(.,'Next')]")).click();
     driver.findElement(By.name("password")).sendKeys("123456");
     WebDriverWait wait = new WebDriverWait(driver, 10);
     WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[contains(.,'Next')]")));
     element.click();

希望它会对你有所帮助:)。

答案 2 :(得分:0)

使用以下代码:

driver.findElement(By.id("identifierId")).sendKeys("testmai@gmail.com");
driver.findElement(By.id("identifierNext")).click();
System.out.println("Username Entered And Next Button clicked");
try
{
    new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//input[@type='password']"))));
    driver.findElement(By.xpath("//input[@type='password']")).sendKeys("testpassword");
}
catch(Exception e)
{
    new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//input[@type='password']"))));
    driver.findElement(By.xpath("//input[@type='password']")).sendKeys("testpassword");

    driver.findElement(By.id("passwordNext")).click();
    System.out.println("Password Entered And Next Button clicked");
}