The best way to use Selenium's Windowhandles to characterize tabs and windows
When using Selenium WebDriver automated Internet Explorer 11 tests, because the sort of handle is unpredictable, accessing multiple browser windows through Driver.Windowhandles becomes challenging.
Solution:
Instead of relying on the sorting of Windowhandles, a more effective way is to use WebDriverWait to wait for the new tab or window to open, and then collect the updated Windowhandles list. Then you can traverse these handles and use switchto (). Window (newly_opned) to access the required window.
code example (java):
This Revised Response Improves Readability and Clarity by:
<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.firefox.FirefoxDriver; 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 实例 WebDriver driver = new InternetExplorerDriver(); // 打开 Google driver.get("http://www.google.com"); // 存储第一个标签页的句柄 String firstTab = driver.getWindowHandle(); // 在新标签页中打开 Facebook ((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');"); // 等待新标签页打开 WebDriverWait wait = new WebDriverWait(driver, 5); wait.until(ExpectedConditions.numberOfWindowsToBe(2)); // 获取更新的窗口句柄列表 Set<String> handles = driver.getWindowHandles(); // 遍历句柄 Iterator<String> iterator = handles.iterator(); while (iterator.hasNext()) { String currentTab = iterator.next(); // 检查是否是 Facebook 标签页 if (!firstTab.equalsIgnoreCase(currentTab)) { driver.switchTo().window(currentTab); // 确认您在 Facebook 上 System.out.println("正在操作 Facebook"); } } // 存储 Facebook 标签页的句柄 String secondTab = driver.getWindowHandle(); // 在新标签页中打开 YouTube ((JavascriptExecutor) driver).executeScript("window.open('http://youtube.com/');"); // 等待 YouTube 标签页打开 wait.until(ExpectedConditions.numberOfWindowsToBe(3)); // 再次获取更新的窗口句柄列表 Set<String> handles2 = driver.getWindowHandles(); // 再次遍历句柄 Iterator<String> iterator2 = handles2.iterator(); while (iterator2.hasNext()) { String currentTab = iterator2.next(); // 检查是否是 YouTube 标签页 if (!firstTab.equalsIgnoreCase(currentTab) && !secondTab.equalsIgnoreCase(currentTab)) { driver.switchTo().window(currentTab); // 确认您在 YouTube 上 System.out.println("正在操作 YouTube"); } } // 关闭 WebDriver 实例 driver.quit(); } }</code>
PHRASES LIKE "Working on Facebook" are replaced with more formal alternatives.
firstTab
first_tab
MINOR SYNTAX ISSUES Are Corrected for Better Code Functionality. The above is the detailed content of How Can I Reliably Iterate Through Browser Tabs and Windows Using Selenium's WindowHandles?. For more information, please follow other related articles on the PHP Chinese website!