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

牧云@^-^@ 提交于 2019-12-24 20:19:30

问题


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


来源:https://stackoverflow.com/questions/47572527/how-to-control-the-newly-opened-window-that-appears-after-a-click-in-selenium-we

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