下面的代码没有处理selenium webdriver中的新窗口

时间:2015-01-13 10:43:15

标签: selenium-webdriver

要处理多个窗口,我在下面写了代码。它没有给我任何错误,但是在新窗口上没有执行任何操作。你能帮我解决一下这个问题,或者建议我用其他方法。谢谢!

代码:

public static void main(String[] args) throws InterruptedException {
    // TODO Auto-generated method stub

    FirefoxDriver dw2 = new FirefoxDriver();

    dw2.get("http://quercus0638v.quercus.kpn.org:90");


    //OPEN login and provide credentials
    dw2.manage().window().maximize();
    Thread.sleep(2000);
    dw2.findElement(By.id("ContentPlaceHolder1_GebruikersnaamTextBox")).clear();        
    dw2.findElement(By.id("ContentPlaceHolder1_GebruikersnaamTextBox")).sendKeys("behdse");   
    dw2.findElement(By.id("ContentPlaceHolder1_WachtwoordTextBox")).clear();
    dw2.findElement(By.id("ContentPlaceHolder1_WachtwoordTextBox")).sendKeys("fiet$hok");

    //Login   : Clcik on Login button
    dw2.findElement(By.id("ContentPlaceHolder1_LoginButton")).click();        


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

     // Below code I written to get the window handles
     Set<String> AllWindowHandles = dw2.getWindowHandles();
        String window1 = (String) AllWindowHandles.toArray()[0];
        System.out.print("window1 handle code = "+AllWindowHandles.toArray()[0]);
        String window2 = (String) AllWindowHandles.toArray()[1];
        System.out.print("\nwindow2 handle code = "+AllWindowHandles.toArray()[1]);


       //Switch to window2(child window) and performing actions on it.
       dw2.switchTo().window(window2);


     //Click the user
     dw2.findElement(By.id("AfdelingGebruikersListView_RowCheckBox_8")).click();

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


   //Switch to window1(child window) and performing actions on it.
     dw2.switchTo().window(window1);

    //Click the order type you want to retrieve
     dw2.findElement(By.id("ContentPlaceHolder1_VerwerktRadioButton")).click();

    //Click for exporting the data to Excel Sheet
     dw2.findElement(By.id("ContentPlaceHolder1_ExporteerNaarExcelButton")).click();

}

2 个答案:

答案 0 :(得分:0)

根据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();

答案 1 :(得分:0)

你可以使用以下方式切换到Windows:

for (String winHandle : objDriver.getWindowHandles()) {
        objDriver.switchTo().window(winHandle);
    }

这将切换到最后打开的窗口。要再次使用上面的代码返回父窗口。最好将代码合并到不同的方法中以便重用。

希望这会有所帮助。感谢。