简体   繁体   中英

Handlng the Multiple Windows in Selenium By using Java

Can anyone please help me to resolve windows handling in selenium webdriver Java.

There are 3 or 4 windows in my application, 2 windows are open successfully using below code: when i want to Execute 3 window with below code its redirect to 1st window,im unable to perform more than 2 windows .

---------------------------------CODE------------------------------------------------------------------------------------------

 String parent=driver.getWindowHandle();

 CompanyClick.click();

 Set<String>s1=driver.getWindowHandles();

 int count=s1.size();

System.out.println("Total window"  +count);

        for(String child:s1)
        {
            if(!parent.equalsIgnoreCase(child)) {
                driver.switchTo().window(child);

                Thread.sleep(3000);

              }
          }

我不确定100%,但在切换到画框之前请尝试

driver.switchTo.defaultContent()

Folks, Below code is working! I found the solution

    String adminWindow = driver.getWindowHandle();

    System.out.println(adminWindow);

    ClickBuissness.click();

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

    Set<String> allWindows = driver.getWindowHandles();

    String agentWindow = null;

    int count =allWindows.size();

    System.out.println("Total window"  + count);

    for(String windowHandle : allWindows){

        if (!windowHandle.equals(adminWindow))

            agentWindow=windowHandle;
    }
        driver.switchTo().window(agentWindow);


        }

Try this.

 public void SwitchToAnotherWindow(Webdriver driver,int window_number){

     List<String> windowlist = null;

    Set<String> windows = driver.getWindowHandles();

    windowlist = new ArrayList<String>(windows);

   String currentWindow = driver.getWindowHandle();

 if (!currentWindow.equalsIgnoreCase(windowlist.get(window_number - 1))) 
    {
        driver.switchTo().window(windowlist.get(window_number - 1));
     }

}

just pass window number which window you want execute for example if you want switch to 3 window just pass 3 to this method,it can directly shuffle to any window (for ex:1 to 9 window,or 9 to 4 window )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM