0

I'm having testcase for handle the popup but control is not going to popup window. It is displaying the getTitle of main window instead of popup window. can you go through below code.

@Test
public void testText1() throws Exception {
    driver.get("http://www.hdfcbank.com");
    Thread.sleep(8000);
    driver.findElement(By.xpath(".//*[@id='loginsubmit']")).click();

    String popupHandle = driver.getWindowHandle();
    WebDriver popup;

    popup = driver.switchTo().window(popupHandle);
    System.out.println(popup.getTitle());
    if (popup.getTitle().equals("netbanking")) {
        System.out.println("I am going to access the elements of popup");
        driver.findElement(By.xpath(".//*[@id='wrapper']/div[6]/a/img")).click();
    } else {
        System.out.println("Worth Trying try harder to get success");
        //   
    }

}

Output: {d0f39d30-49e7-4203-b9ef-10380fbfcb5e} HDFC Bank: Personal Banking Services I am going to access the elements of popup Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//*[@id='wrapper']/div[6]/a/img"} Command duration or timeout: 30.15 seconds

4

1 回答 1

2

你在正确的轨道上。该driver.getWindowHandle()方法仅返回当前窗口的句柄,这将是您的主窗口。如果打开另一个弹出窗口,您将需要调用Set<String> handles = driver.getWindowHandles()以返回所有可用句柄的列表。然后就可以调用了driver.switchTo().window(handles.get(handles.size() - 1))。这将切换到最后列出的(最新的)窗口句柄。

您也不需要声明新的 WebDriver 对象。通过调用switchTo(),您将驱动程序的焦点转移到新窗口。确保在进行切换之前保存对主窗口句柄的引用,以便之后能够切换回主窗口。

String mainHandle = driver.getWindowHandle();
String[] handles = driver.getWindowHandles().toArray(new String[0]);
driver.switchTo().window(handles[handles.length - 1]);
...
driver.close(); //close the popup window
driver.switchTo().window(mainHandle);
于 2013-09-24T19:46:59.197 回答