Selenium 测试脚本通过新的 ajax 登录表单登录到谷歌帐户 [英] Selenium test scripts to login into google account through new ajax login form
问题描述
我能够编写脚本将我的电子邮件地址添加到电子邮件元素中.但是一旦通过脚本点击下一步,谷歌就会使用 ajax 动态地将该电子邮件元素替换为密码元素.这就是我被卡住的地方,无法在该元素中提供密码且无法登录.
I am able to write scripts to give my email address into email element. But once click next through scripts Google uses ajax to replace that email element to password element dynamically. This is where I am stuck and won't be able to give password in that element and no login.
网址:https://accounts.google.com/signin/v2/identifier?flowName=GlifWebSignIn&flowEntry=ServiceLogin
请编写 selenium 测试脚本来实现这一点.
Please write the selenium test scripts to achieve this.
推荐答案
这是访问 url https://accounts.google.com/signin
使用您的有效凭据登录的代码块和在控制台上打印 Page Title
:
Here is the code block to access the url https://accounts.google.com/signin
login with your valid credentials and print the Page Title
on your console:
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 日)
优化上面的代码块并添加几个可以使用的参数:
Update(5-Jan-2020)
Optimizing the above code block and adding a couple of arguments you can use:
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();
}
}
这篇关于Selenium 测试脚本通过新的 ajax 登录表单登录到谷歌帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!