Selenium webdriver 下面的代码不是SeleniumWebDriver中的新窗口

Selenium webdriver 下面的代码不是SeleniumWebDriver中的新窗口,selenium-webdriver,Selenium Webdriver,为了处理多个窗口,我编写了以下代码。它没有给我任何错误,但是在新窗口上没有执行任何操作。你能帮我解决这个问题,或者给我任何其他的建议吗。谢谢 代码: publicstaticvoidmain(String[]args)抛出InterruptedException{ //TODO自动生成的方法存根 FirefoxDriver dw2=新的FirefoxDriver(); dw2.get(“http://quercus0638v.quercus.kpn.org:90"); //打开登录并提供凭据 d

为了处理多个窗口,我编写了以下代码。它没有给我任何错误,但是在新窗口上没有执行任何操作。你能帮我解决这个问题,或者给我任何其他的建议吗。谢谢

代码:

publicstaticvoidmain(String[]args)抛出InterruptedException{
//TODO自动生成的方法存根
FirefoxDriver dw2=新的FirefoxDriver();
dw2.get(“http://quercus0638v.quercus.kpn.org:90");
//打开登录并提供凭据
dw2.manage().window().maximize();
《睡眠》(2000年);
dw2.findElement(By.id(“ContentPlaceHolder1_GebruikersnaamTextBox”).clear();
dw2.findElement(By.id(“ContentPlaceHolder1_GebruikersnaamTextBox”)).sendKeys(“behdse”);
dw2.findElement(By.id(“contentplaceholder 1_WachtwoordTextBox”)).clear();
dw2.findElement(By.id(“contentplaceholder 1_WachtwoordTextBox”)).sendKeys(“fiet$hok”);
//登录:点击登录按钮
dw2.findElement(By.id(“ContentPlaceholder 1_LoginButton”))。单击();
//点击选择按钮。之后,新窗口将打开
dw2.findElement(By.id(“contentplaceholder 1_CWDatumRadioButton”))。单击();
//下面是我为获取窗口句柄而编写的代码
设置AllWindowHandles=dw2.getWindowHandles();
字符串window1=(字符串)AllWindowHandles.toArray()[0];
System.out.print(“window1句柄代码=“+AllWindowHandles.toArray()[0]);
字符串window2=(字符串)AllWindowHandles.toArray()[1];
System.out.print(“\nWindows 2句柄代码=“+AllWindowHandles.toArray()[1]);
//切换到window2(子窗口)并对其执行操作。
dw2.switchTo().window(window2);
//单击用户
dw2.findElement(By.id(“AfdelingGebruikersListView_RowCheckBox_8”))。单击();
//选择用户后单击OK按钮
dw2.findElement(By.id(“AfdelingGebruikersListView_-OKButton”)。单击();
//切换到window1(子窗口)并对其执行操作。
dw2.switchTo().window(window1);
//单击要检索的订单类型
dw2.findElement(By.id(“contentplaceholder 1_VerwerktRadioButton”))。单击();
//单击以将数据导出到Excel工作表
dw2.findElement(By.id(“ContentPlaceHolder1\u ExporteerNaarExcelButton”)。单击();
}
根据OP的评论编辑代码:- 因为,正如我在评论中所建议的,这可能不是一个新窗口。它必须是一个弹出窗口。因此,请按以下方式替换代码,然后再试一次:

//Click on Select button.  After that new window will open
dw2.findElement(By.id("ContentPlaceHolder1_CWDatumRadioButton")).click()

//Click the user
try{
    WebDriverWait wait = new WebDriverWait(dw2,20);
    WebElement ele = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("AfdelingGebruikersListView_RowCheckBox_8")));
    ele.click();
}catch(Throwable e){
    System.err.println("Error while clicking on the element. "+e.getMessage());
}

//Click on OK button after selecting User
dw2.findElement(By.id("AfdelingGebruikersListView_OKButton")).click();

您可以使用以下方式切换到windows:

for (String winHandle : objDriver.getWindowHandles()) {
        objDriver.switchTo().window(winHandle);
    }
这将切换到上次打开的窗口。再次使用上述代码返回父窗口。为了重用,最好将代码合并到不同的方法中


希望这有帮助。谢谢。

朋友们好,很遗憾,上面的代码不起作用。不显示错误消息,但不识别第二个窗口。我想与大家分享一些更多的细节,以帮助了解我所面临的确切问题。下面是我的观察结果:当创建新窗口时,它在任务栏上显示浏览器窗口的两个实例,如下所示,在红色框中高亮显示:但是在我的情况下,新窗口打开时,任务栏仍然只显示浏览器的一个实例。我不确定这是一个框架还是一个新窗口。你能帮我解决这个问题吗?谢谢如果这是一个公共站点,请共享URL。否则,请添加与窗口相关的图像。因为,你没有足够的声誉,请上传你的图片,并在这里分享链接。这样就更容易理解你的意思了。谢谢。:)嗨Subh,我不能上传这个网站上的图像,因为这阻止了我。有没有别的办法,我可以把附件寄给你。请让我知道你的想法。非常感谢你的帮助,就像我说的,上传截图/附件并在这里添加链接,这样我就可以访问它。我已经上传了上述两个观察的图像。请看一看。
for (String winHandle : objDriver.getWindowHandles()) {
        objDriver.switchTo().window(winHandle);
    }