Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用selenium处理新弹出窗口_Selenium_Selenium Webdriver - Fatal编程技术网

如何使用selenium处理新弹出窗口

如何使用selenium处理新弹出窗口,selenium,selenium-webdriver,Selenium,Selenium Webdriver,我目前正在使用selenium自动化一个应用程序,当单击一个按钮时,会出现一个新的弹出窗口。我必须切换到该窗口并执行某些操作,例如搜索记录 注意父窗口和子窗口具有相同的标题。您可以使用此标题,您必须提供要切换的窗口的索引 switchToWindowByIndex(1); public void switchToWindowByIndex(int wndIndex) { Set<String> handles = Driver.getWindowHandles();

我目前正在使用selenium自动化一个应用程序,当单击一个按钮时,会出现一个新的弹出窗口。我必须切换到该窗口并执行某些操作,例如搜索记录


注意父窗口和子窗口具有相同的标题。

您可以使用此标题,您必须提供要切换的窗口的索引

switchToWindowByIndex(1);

public void switchToWindowByIndex(int wndIndex) {
    Set<String> handles = Driver.getWindowHandles();
    if (handles.size() > wndIndex) {
        String handle = handles.toArray()[wndIndex].toString();
        Driver.switchTo().window(handle);
    } else {
        throw new RuntimeException("There are only [" + handles.size() + "] windows present at the moment.Requested window is [" + wndIndex + "] which is out of range");
    }
}
切换到indowbyindex(1);
公共无效切换到INDOWBYINDEX(int wndIndex){
Set handles=Driver.getWindowHandles();
if(handles.size()>wndIndex){
字符串句柄=句柄.toArray()[wndIndex].toString();
Driver.switchTo().窗口(手柄);
}否则{
抛出新的RuntimeException(“目前只有[“+句柄.size()+”]个窗口。请求的窗口为[“+wndIndex+”],超出范围”);
}
}

您可以通过index切换到新窗口Hanks Madhan,我是selenium的新手,您能给我一个例子说明如何在其中使用index吗。