0

我使用硒网络驱动程序。我打开了一个父窗口。单击链接后,将打开新窗口。我从列表中选择一些值,此窗口会自动关闭。现在我需要在我的父窗口中操作。我怎样才能做到这一点?我尝试了以下代码:

String HandleBefore = driver.getWindowHandle();
driver.findElement(By.xpath("...")).click();
 for (String Handle : driver.getWindowHandles()) {
        driver.switchTo().window(Handle);}
 driver.findElement(By.linkText("...")).click();
 driver.switchTo().window(HandleBefore);

但这不起作用。

4

2 回答 2

1

在单击打开新窗口的链接之前尝试此操作:

parent_h = browser.current_window
# click on the link that opens a new window
handles = browser.window_handles # before the pop-up window closes
handles.remove(parent_h)
browser.switch_to_window(handles.pop())
# do stuff in the popup
# popup window closes
browser.switch_to_window(parent_h)
# and you're back
于 2012-07-10T04:39:42.810 回答
0

公共布尔开关BackParentWindowTitle(字符串窗口标题){

    boolean executedActionStatus = false;
    try {

        Thread.sleep(1000);
        WebDriver popup = null;
        Set<String> handles = null;

        handles =driver.getWindowHandles();
        Iterator<String> it = handles.iterator();
        while (it.hasNext()){

            String switchWin = it.next();
            popup = driver.switchTo().window(switchWin);

            System.out.println(popup.getTitle());
            if(popup.getTitle().equalsIgnoreCase(windowTitle)){

                log.info("Current switched window title is "+popup.getTitle());
                log.info("Time taken to switch window is "+sw.elapsedTime());
                executedActionStatus = true;
                break;
            }
            else
                log.info("WindowTitle does not found to switch over");
        }
    }
    catch (Exception er) {
        er.printStackTrace();
        log.error("Ex: "+er);
    }   
    return executedActionStatus;
}
于 2013-03-29T16:41:33.190 回答