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

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

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

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

以下是使用您的有效凭据访问urlhttps://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 

下面的代码适合我

  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(); 

希望它能帮到你:)

使用以下代码:

 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"); }