Divya Master
Divya Master

Reputation: 79

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

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

Upvotes: 2

Views: 6344

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193188

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 :

snapshot

Upvotes: 1

Devendra_P
Devendra_P

Reputation: 91

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 

Upvotes: 1

Related Questions