user9012813
user9012813

Reputation:

How to control the newly opened window that appears after a click in Selenium WebDriver?

I was just trying to automate the Gmail login process using Selenium WebDriver...After the email id is entered a new page opens to enter the password. How do I enter the password?

WebElement element= driver.findElement(By.xpath("//*[@id='ident']"));
element.sendKeys("THE EMAIL ID");
element.sendKeys(Keys.ENTER);
WebElement ele= driver.findElement(By.xpath("//*[@id='passd']"));
ele.sendKeys("THE PASSWORD");
ele.sendKeys(Keys.ENTER);

Upvotes: 0

Views: 278

Answers (1)

Mahmud Riad
Mahmud Riad

Reputation: 1165

You have to switch the driver to current handler.

Please use below code to enter password.

   WebElement element= driver.findElement(By.xpath("//*
   [@id='ident']"));
   element.sendKeys("THE EMAIL ID");
   element.sendKeys(Keys.ENTER);

   String mainWindowHandler = driver.getWindowHandle();

   for(String winHandle : driver.getWindowHandles()) {
       if (!mainWindowHandler.equals(winHandle)) {
           driver.switchTo().window(winHandle);
       }
   }

   WebElement ele = driver.findElement(By.xpath("//*[@id='passd']"));
   ele.sendKeys("THE PASSWORD");
   ele.sendKeys(Keys.ENTER);

Upvotes: 1

Related Questions