Selenium webdriver Java处理超过2个子窗口

时间:2015-04-02 01:33:57

标签: java selenium-webdriver

我有一个基于Web的应用程序,它为我在该特定新窗口中执行的每个操作启动子窗口。例如:父窗口(parentButton) - > Child1窗口(child1Button) - > Child2窗口(child2Button)

我单击父窗口中的Pbutton将启动子窗口-1。我在Child窗口中单击Cbutton1将启动Child Window-2。我在Child window-2中单击Cbutton2,它将启动Child Window-3。

所以,我假设我已启动浏览器应用程序,我必须切换到Child Window-1,执行cbutton-1,切换到Child window-2,执行cbutton-2操作,切换到子窗口-3并在此窗口的文本区域中发送一些文本。

这是我的Java代码,

    /* driver launches and logs in to the Parent window */
    String winHandleConsole = driver.getWindowHandle();
    log.info("Title of Console window: " +driver.getTitle());
    driver.findElement(Constants.lnkIncidentManagement).click();
    Set<String> strHandles = driver.getWindowHandles();
    log.info("Size of window handle: "+strHandles.size());
    for(String handle: strHandles){
        driver.switchTo().window(handle);
        String strTitle = driver.getTitle();
        if(strTitle.equalsIgnoreCase("Incident Management Console(Search)")){
                driver.manage().timeouts().implicitlyWait(TimeOutInSec, TimeUnit.SECONDS);
                driver.findElement(Constants.lnkNewIncident).click();
                Set<String> strHandle_2 = driver.getWindowHandles();
                log.info("NEW WINDOW HANDLES: "+ strHandle_2.size());
                for(String handle_2: strHandle_2){
                    log.info("-------:" + driver.getTitle());
                    if(driver2.getTitle().equalsIgnoreCase("Incident (New)")){
                        log.info("Title of Incident-New: "+ driver2.getTitle());
                        driver.switchTo().window(handle_2);
                        driver.manage().timeouts().implicitlyWait(TimeOutInSec, TimeUnit.SECONDS);
                        driver.findElement(Constants.txtNewIncidentSummary).sendKeys("This is a text");

                    }
                }
            }`

我看到一个标题为“事件(新)”的新窗口,当我打印出strHandle_2的大小时,我看到3个被引用。但是,当我打印handle_2变量时,我看不到标题打印。

如何从父级切换到child1以及从child1切换到child2?

2 个答案:

答案 0 :(得分:0)

包装硒;

导入org.openqa.selenium.By;

导入org.openqa.selenium.WebDriver;

导入org.openqa.selenium.WebElement;

导入org.openqa.selenium.chrome.ChromeDriver;

导入org.openqa.selenium.firefox.FirefoxDriver;

导入org.openqa.selenium.interactions.Action;

导入org.openqa.selenium.interactions.Actions;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.Set;

导入java.util.concurrent.TimeUnit;

导入org.openqa.selenium。*;

公共类BookToCart

{

public static void main(String[] args) throws InterruptedException {

    //Instantiating FirefoxDriver

     System.setProperty("webdriver.chrome.driver", "D:\\selenium\\chromedriver.exe");

     WebDriver driver = new ChromeDriver();

    //Maximize the browser

    driver.manage().window().maximize();

    //Implicit wait for 10 seconds

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    //To launch softwaretestingmaterial.com

    driver.get("http://wikipedia.com");

WebElement链接= driver.findElement(By.id(“ js-link-box-es”));

Actions newwin = new Actions(driver);

newwin.keyDown(Keys.SHIFT).click(link).keyUp(Keys.SHIFT).build()。perform();

Thread.sleep(6000);

WebElement link2 = driver.findElement(By.id(“ js-link-box-it”));

Actions newwin2 = new Actions(driver);

newwin.keyDown(Keys.SHIFT).click(link2).keyUp(Keys.SHIFT).build()。perform();

Thread.sleep(6000);

WebElement link3 = driver.findElement(By.id(“ js-link-box-en”));

Actions newwin3 = new Actions(driver);

newwin.keyDown(Keys.SHIFT).click(link3).keyUp(Keys.SHIFT).build()。perform();

Thread.sleep(6000);

int count = 0;

String win = driver.getWindowHandle();

ArrayList allWindows =新的ArrayList(driver.getWindowHandles());

    //for( String child_window:allwindows)

    //{

        //driver.switchTo().window(child_window);

    //}

driver.switchTo()。window(allWindows.get(1));

Thread.sleep(6000);

WebElement about = driver.findElement(By.xpath(“ // * [@ title ='了解维基百科']”)));

about.click();

}

}

答案 1 :(得分:-1)

假设您已经在使用两个窗口,现在您想要单击一个链接,该链接将打开第三个窗口并且您想要打开第三个窗口: 我写了一个简单的逻辑和它的工作................ 将WebElement传递到要单击的位置..............

public static void switchToWindow(WebElement ele)
    {
        try {
            Set<String> OldAllWindows = GenericMethods.driver.getWindowHandles();
            ele.click();
            Set<String> NewAllWindows = GenericMethods.driver.getWindowHandles();
            for(String newWindow : NewAllWindows)
            {
                if(!OldAllWindows.contains(newWindow))
                {
                    GenericMethods.driver.switchTo().window(newWindow);
                }
            }

            //  
        } catch (Exception e) {
            System.out.println("Issue in Switching Window"+e.getMessage());
        }

    }