一尘不染

selenium测试脚本通过新的Ajax登录表单登录到Google帐户

selenium

我能够编写脚本以将我的电子邮件地址添加到email元素中。但是一旦通过脚本单击“下一步”,Google就会使用ajax将该电子邮件元素动态替换为密码元素。这是我遇到的问题,无法在该元素中提供密码并且无法登录。

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

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


阅读 384

收藏
2020-06-26

共1个答案

一尘不染

这是https://accounts.google.com/signin使用您的有效凭据访问url 登录并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

更新(2020年1月5日)

优化上面的代码块并添加几个可以使用的参数:

public class browserAppDemo 
{
    public static void main(String[] args) throws Exception 
    {
        System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("start-maximized");
        options.setExperimentalOption("useAutomationExtension", false);
        options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
        WebDriver driver =  new ChromeDriver(options); 
        driver.get("https://accounts.google.com/signin")
        new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='identifierId']"))).sendKeys("emailID");
        driver.findElement(By.id("identifierNext")).click();
        new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@name='password']"))).sendKeys("password");
        driver.findElement(By.id("passwordNext")).click();
    }
}
2020-06-26