How to control the newly opened window that appear

2019-08-18 19:14发布

问题:

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

回答1:

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