如何关闭Child Windows?在Selenium WebDriver中 - 使用FunctionalClass

时间:2013-08-27 07:03:01

标签: java selenium-webdriver

如何关闭Child Windows?在Selenium WebDriver中 - 使用FunctionalClass 嗨,我正在使用Selenium Web驱动程序和框架工作,在我的应用程序中单击

一个链接按钮,然后子窗口将在屏幕上显示,现在如何关闭

儿童Windows。

public class Reports
{
    public void RPS(WebDriver driver) throws Exception
    {
        AllpagesLogins ALP= new AllpagesLogins();
        //Clicking on the Reports Menu Btn-- Use for Menu Button upper and down
        WebElement element1 = driver.findElement(By.id(ALP.RP_Reports_MenuBtn_ID));
        ((JavascriptExecutor) driver).executeScript("arguments[0].click()", element1);
        System.out.println("Clicked on the Reports Menu Btn");
        Thread.sleep(1000);
        //Clicking on the Particular Link Button
        if(driver.findElement(By.linkText("Congratulations Sandeep Pushpala!!!")).isDisplayed())
        {
            System.out.println("Link Button is Displayed");
            driver.findElement(By.linkText("Congratulations Sandeep Pushpala!!!")).click();
            System.out.println("Clicked on the Link Button Which is Displayed");
            Thread.sleep(2000);
        }
        else
        {
            System.out.println("Link Button is Not Displayed");
        }
        //clicking on the First Link Button, It will display the First Child Window
        ////////////////////////////11111111111111
        if(driver.findElement(By.id(ALP.RP_TotalRecipients_LnkBtn_ID)).isEnabled())
        {
            System.out.println("LnkBtnTotalRecipientsID Link Button is Enabled");
            driver.findElement(By.id(ALP.RP_TotalRecipients_LnkBtn_ID)).click();
            System.out.println("Clicked on the LnkBtnTotalRecipientsID Button");
            Thread.sleep(2000);
        }
        else
        {
            System.out.println("Link Button is Total Recipients is Not Displayed");
        }
        //clicking on the Second Link Button, It will display the Second Child Window
        ////////////////////////////2222222222222222222
        if(driver.findElement(By.id(ALP.RP_SuccessfullRecipientsData_LnkBtn_ID)).isEnabled())
        {
            System.out.println("SuccessfullRecipientsDataID Link Button is Enabled");
            driver.findElement(By.id(ALP.RP_SuccessfullRecipientsData_LnkBtn_ID)).click();
            System.out.println("Clicked on the LinkSuccessfullRecipientsDataID Button");
            Thread.sleep(2000);
        }
        else
        {
            System.out.println("Link Button is SuccessfullRecipientsDataID is Not Displayed");
        }
        //clicking on the Third Link Button, It will display the Third ![enter image description here][1]Child Window
        ///////////////////////////////333333333333333333333333333
        if(driver.findElement(By.id(ALP.Rp_NotSucessfullRecipientsData_LnkBtn_ID)).isEnabled())
        {
            System.out.println("NotSucessfullRecipientsDataID Link Button is Enabled");
            driver.findElement(By.id(ALP.Rp_NotSucessfullRecipientsData_LnkBtn_ID)).click();
            System.out.println("Clicked on the LinkNotSucessfullRecipientsDataID Button");
            Thread.sleep(2000);
        }
        else
        {
            System.out.println("Link Button is Successfully Recipients Data ID is Not Displayed");
        }
        for Closing the Child Windows I am Using This Code
        //Closing the First Child Window
        driver.close();
        System.out.println("First Child Window is Closed");
        Thread.sleep(2000);
        //Closing the Second Child Window
        driver.close();
        System.out.println("Second Child Window is Closed");
        Thread.sleep(2000);
        //Closing the Third Child Window
        driver.close();
        System.out.println("Third Child Window is Closed");
        Thread.sleep(2000);

3 个答案:

答案 0 :(得分:1)

我有一个实用工具方法可以切换到所需的窗口,如下所示

public class Utility 
{
    public static WebDriver getHandleToWindow(String title){

        //parentWindowHandle = WebDriverInitialize.getDriver().getWindowHandle(); // save the current window handle.
        WebDriver popup = null;
        Set<String> windowIterator = WebDriverInitialize.getDriver().getWindowHandles();
        System.err.println("No of windows :  " + windowIterator.size());
        for (String s : windowIterator) {
          String windowHandle = s; 
          popup = WebDriverInitialize.getDriver().switchTo().window(windowHandle);
          System.out.println("Window Title : " + popup.getTitle());
          System.out.println("Window Url : " + popup.getCurrentUrl());
          if (popup.getTitle().equals(title) ){
              System.out.println("Selected Window Title : " + popup.getTitle());
              return popup;
          }

        }
                System.out.println("Window Title :" + popup.getTitle());
                System.out.println();
            return popup;
        }
}

一旦窗口的标题作为参数传递,它将带您进入所需的窗口。 在你的情况下,你可以做到。

Webdriver childDriver = Utility.getHandleToWindow("titleOfChildWindow");
childDriver.close();

然后再次使用相同的方法切换到父窗口

Webdriver parentDriver = Utility.getHandleToWindow("titleOfParentWindow");

此方法在处理多个窗口时有效。

答案 1 :(得分:0)

您必须使用switchTo() method切换到另一个窗口:

String firstWindow = driver.getWindowHandle();
// execute code to open a second window

// close the first window
driver.switchTo().window(firstWindow).close();

答案 2 :(得分:0)

    List<String> windows = new ArrayList<>(driver.getWindowHandles());//Returning Set
    for (String s: windows) { System.out.println("window: " + s);} // Let's see names
    String parentWindow = windows.get(0); //In my case, I have two windows.
    String childWindow = windows.get(1); 

    driver.switchTo().window(parentWindow).close();
    driver.switchTo().window(childWindow).close();