Best way to track and iterate over tabs and windows using WindowHandles using Selenium
P粉744691205
P粉744691205 2023-11-04 18:03:14
0
1
600

We are working with Selenium webdriver for UI testing for Internet Explorer 11. In the test web application, several screens pop up. In some tests we ended up with three browser windows and therefore three Driver.WindowHandles. To switch from one WindowHandle to another, we expect Driver.WindowHandles to be sorted with the oldest window first and the newest window last. But that's not the case: it's completely random!

Because windowhandle is a GUID, we end up creating a dictionary with the WindowHandle GUID as the key and the value as the value for the type of page loaded in the browser window. But this will also cause the dictionary to be maintained when the window is closed.

Seems like a lot of work for such a simple thing. Is there a better solution?

P粉744691205
P粉744691205

reply all (1)
P粉986937457

You are absolutely right:

In a discussion, Simon clearly mentioned:

So we will raiseWebDriverWait, then collect the window handle every time a new tab/window is opened, and finally iterate over the window handle andswitchTo().window( newly_opened)As needed:

Java:

package demo; import java.util.Iterator; import java.util.Set; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class NEW_TAB_Handling { public static void main(String[] args) { System.setProperty("webdriver.ie.driver", "C:\Utility\BrowserDrivers\IEDriverServer.exe"); WebDriver driver = new InternetExplorerDriver(); driver.get("http://www.google.com"); String first_tab = driver.getWindowHandle(); System.out.println("Working on Google"); ((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');"); WebDriverWait wait = new WebDriverWait(driver,5); wait.until(ExpectedConditions.numberOfWindowsToBe(2)); Set s1 = driver.getWindowHandles(); Iterator i1 = s1.iterator(); while(i1.hasNext()) { String next_tab = i1.next(); if (!first_tab.equalsIgnoreCase(next_tab)) { driver.switchTo().window(next_tab); System.out.println("Working on Facebook"); } } String second_tab = driver.getWindowHandle(); ((JavascriptExecutor) driver).executeScript("window.open('http://youtube.com/');"); wait.until(ExpectedConditions.numberOfWindowsToBe(3)); Set s2 = driver.getWindowHandles(); Iterator i2 = s2.iterator(); while(i2.hasNext()) { String next_tab = i2.next(); if (!first_tab.equalsIgnoreCase(next_tab) && !second_tab.equalsIgnoreCase(next_tab)) { driver.switchTo().window(next_tab); System.out.println("Working on Youtube"); } } driver.quit(); System.out.println("Quit the WebDriver instance"); } }

Console output:

Working on Google Working on Facebook Working on Youtube Quit the WebDriver instance

other

You can find discussions based onatOpen Web Selenium Python in a new tabp>

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!