How to handle 'Log in with Google' popup window using Selenium WebDriver

梦想的初衷 提交于 2021-02-15 07:29:52

问题


So i have a piece of code which works on fblogin pop-up window but the same piece of code doesn't work on googlelogin popup window. I dont know why. Website = https://accounts.trivago.com/login#

Fb Sign Up code :

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

回答1:


While you access the Website https://accounts.trivago.com/login# you have to take help of a Locator Strategy which uniquely identifies the button Log in with Google then invoke click() method on it and finally induce WebDriverWait before switching over to the Gmail Login Box. You can use the following code block :

Code Block :

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

Console Output :

Parent Window ID is : 4294967297
Working on Google Login Box

Snapshot :




回答2:


Your code is correct , only need to add wait after click on "Log in with Google" button.

You can also use below code

    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 


来源:https://stackoverflow.com/questions/49624260/how-to-handle-log-in-with-google-popup-window-using-selenium-webdriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!