您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

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

这是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

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

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();
    }
}
Go 2022/1/1 18:14:47 有577人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶