Introduction
When using the SELENIUM Webdriver for the UI test of the Internet Explorer 11, managing multiple labels and windows may be tricky. In order to effectively track them, the use of attributes is very important. However, when the expected order of the window handle is unstable, common problems occur. This article will introduce how to solve this problem and explore a more effective solution.
Randomness of the order of the window handle WindowHandles
In contrast to expectations, the order of is not stable. Webdriver implements iteration in any way without any guarantee for insertion order. This may make it difficult to effectively manage multiple windows and switch between them.
Manage the dictionary WindowHandles
GUID as the key, and the corresponding page type as a value. This method ensures correct switching between the windows. However, it requires additional maintenance, especially when closing the window. Introduction to webdriverwait
WindowHandle
A better method is to use
to switch to the required window. Example implementation
WebDriverWait
The following is an example implementation in Java: WindowHandles
switchTo().window(newly_opened)
list. Conclusion
Although it is feasible to use the dictionary window management, the introduction of
<code class="language-java">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.ie.InternetExplorerDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class 新标签页处理 { public static void main(String[] args) { WebDriver driver = new InternetExplorerDriver(); driver.get("http://www.google.com"); String first_tab = driver.getWindowHandle(); ((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');"); WebDriverWait wait = new WebDriverWait(driver, 5); wait.until(ExpectedConditions.numberOfWindowsToBe(2)); Set<String> s1 = driver.getWindowHandles(); Iterator<String> i1 = s1.iterator(); String next_tab; while (i1.hasNext()) { next_tab = i1.next(); if (!first_tab.equalsIgnoreCase(next_tab)) { driver.switchTo().window(next_tab); } } } }</code>
when creating a new tab page provides a more efficient and more window pages and windows in Selenium to track and iterates multiple tab pages and windows. Flexible solutions. WindowHandles
The above is the detailed content of How to Efficiently Track and Iterate Through Selenium WindowHandles in Unpredictable Order?. For more information, please follow other related articles on the PHP Chinese website!