所以我有一段代码可以在 fblogin 弹出窗口上运行,但同一段代码不能在 googlelogin 弹出窗口上运行。我不知道为什么。网站 = https://accounts.trivago.com/login#
Facebook 注册码:
driver.findElement(By.xpath(".//*[@id='authentication-login']/div/section[1]/div[3]/div/button[1]")).click();
String parentWindow = driver.getWindowHandle();
System.out.println("Parent Window ID is : " + parentWindow);
Set<String> allWindow = driver.getWindowHandles();
int count = allWindow.size();
System.out.println("Total Window : " + count);
for(String child:allWindow)
{
if(!parentWindow.equalsIgnoreCase(child))
{
driver.switchTo().window(child);
driver.manage().window().maximize();
driver.findElement(By.id("email")).sendKeys("");
driver.findElement(By.id("pass")).sendKeys("");
driver.findElement(By.id("u_0_0")).click();
Thread.sleep(7000);
}
}
driver.switchTo().window(parentWindow);
谷歌登录:
driver.findElement(By.xpath(".//*[@id='authentication-login']/div/section[1]/div[3]/div/button[2]")).click();
String parentWindow = driver.getWindowHandle();
System.out.println("Parent Window ID is : " + parentWindow);
Set<String> allWindow = driver.getWindowHandles();
int count = allWindow.size();
System.out.println("Total Window : " + count);
for(String child:allWindow)
{
if(!parentWindow.equalsIgnoreCase(child))
{
driver.switchTo().window(child);
driver.manage().window().maximize();
Thread.sleep(7000);
}
}
driver.switchTo().window(parentWindow);
最佳答案
您的代码是正确的,只需在点击“使用 Google 登录”按钮后添加等待即可。
您还可以使用下面的代码
public void LoginGoogle() throws InterruptedException
{
driver.get("https://accounts.trivago.com/login#");
driver.findElement(By.xpath(".//*[@id='authentication-login']/div/section[1]/div[3]/div/button[2]")).click();
Thread.sleep(5000);
String parentWindow = driver.getWindowHandle();
System.out.println("Parent Window ID is : " + parentWindow);
for(String winHandle : driver.getWindowHandles())
{
driver.switchTo().window(winHandle);
System.out.println(driver.getTitle());
driver.manage().window().maximize();
}
WAIT.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='identifierId']")));
driver.findElement(By.xpath("//input[@id='identifierId']")).sendKeys(emailid);
//Continue
关于java - 如何使用 Selenium WebDriver 处理 'Log in with Google' 弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49624260/