如何使用Selenium WebDriver处理“使用Google登录”弹出窗口

时间:2018-04-03 07:12:37

标签: java selenium selenium-webdriver webdriver

所以我有一段代码可以在fblogin弹出窗口中运行,但是同一段代码在googlelogin弹出窗口中不起作用。我不知道为什么。网站= https://accounts.trivago.com/login#

Fb注册码:

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

的GoogleLogin:

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

2 个答案:

答案 0 :(得分:0)

您的代码是正确的,只需点击“使用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 

答案 1 :(得分:0)

当您访问网站click()时,您必须获取定位策略的帮助,该唯一标识按钮使用Google登录然后调用{{1在它上面的方法,最后在切换到 Gmail登录框之前引导 WebDriverWait 。您可以使用以下代码块:

代码块:

    System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
    WebDriver driver = new FirefoxDriver();
    driver.get("https://accounts.trivago.com/login#");
    String parentWindow = driver.getWindowHandle();     
    System.out.println("Parent Window ID is : " + parentWindow);
    driver.findElement(By.xpath("//button[@class='btn social-login__btn social-login__btn--google btn--reset block js_tlgGoogleSignin']//span[@class='btn__text']")).click();
    WebDriverWait wait = new WebDriverWait(driver,5);
    wait.until(ExpectedConditions.numberOfWindowsToBe(2));
    Set<String> s1 = driver.getWindowHandles();
    Iterator<String> i1 = s1.iterator();
    while(i1.hasNext())
    {
        String next_tab = i1.next();
        if (!parentWindow.equalsIgnoreCase(next_tab))
        {
        driver.switchTo().window(next_tab);
        System.out.println("Working on Google Login Box");
        WebDriverWait wait2 = new WebDriverWait(driver, 20);
        wait2.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='identifierId']"))).sendKeys("Divya Master");
        }
    }

控制台输出:

Parent Window ID is : 4294967297
Working on Google Login Box

快照:

snapshot