【问题标题】:Selenium webdriver Java handle more than 2 child windowSelenium webdriver Java处理超过2个子窗口
【发布时间】:2015-04-02 01:33:56
【问题描述】:

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

我在父窗口中单击 P 按钮将启动子窗口 1。我在子窗口 1 中单击 Cbutton1 将启动子窗口 2。我在子窗口 2 中单击 Cbutton2,它将启动子窗口 3。

所以,我假设我已经启动了浏览器应用程序,我必须切换到子窗口 1,执行 cbutton-1,切换到子窗口 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 prited。但是,当我打印 handle_2 变量时,我没有看到打印的标题。

从parent切换到child1,从child1切换到child2应该怎么做?

【问题讨论】:

  • 你能发布你正在处理的html吗?
  • @LittlePanda,这个问题不需要 html。我的确切问题在我原来的帖子的最后一行清楚地提到了。此外,html 太大,无法附加。
  • 我发现了一个类似的问题 - stackoverflow.com/questions/18379410/…
  • 如果您知道窗口名称,请使用switchTo().window("name")

标签: java selenium-webdriver


【解决方案1】:
package selenium;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;

public class 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 link = 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<String> allWindows = new ArrayList<String> (driver.getWindowHandles());


        driver.switchTo().window(allWindows.get(1));
        Thread.sleep(6000);

        WebElement about = driver.findElement(By.xpath("//*[@title='Find out about Wikipedia']"));
        about.click();
    }
}

【讨论】:

    【解决方案2】:

    假设您已经在使用两个窗口,现在您想要单击一个链接,该链接将打开第三个窗口,并且您想要打开第三个窗口: 我写了一个简单的逻辑及其工作...... 传递要点击的 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());
            }
    
        }
    

    【讨论】:

    • GenericMethods是什么意思
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    • 1970-01-01
    相关资源
    最近更新 更多