What does WindowHandles mean in IWebDriver of Selenium?
What does WindowHandles mean in IWebDriver of Selenium?
In Selenium, a WindowHandle
is a unique identifier assigned to each browser window or tab that is opened by the WebDriver instance. When a new window or tab is opened, it is assigned a new WindowHandle
.
The IWebDriver
interface in Selenium provides a WindowHandles
property which returns a collection of string
values representing the unique WindowHandle
for each open window or tab.
This property can be used to switch between windows or tabs, by specifying the desired WindowHandle
to activate. For example, you can use the SwitchTo()
method to switch to a new window or tab like this:
// Get the list of all available window handles
var windowHandles = driver.WindowHandles;
// Switch to the first window
driver.SwitchTo().Window(windowHandles[0]);
// Switch to the second window
driver.SwitchTo().Window(windowHandles[1]);
// Switch back to the first window
driver.SwitchTo().Window(windowHandles[0]);
Could you show the example which include open two windows or tabs
Sure! Here's an example in C# that demonstrates how to open two browser windows and switch between them using the WindowHandles
property:
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
namespace SeleniumDemo
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new ChromeDriver();
// Open the first window
driver.Navigate().GoToUrl("https://www.google.com");
Console.WriteLine("Title of first window: " + driver.Title);
// Open a new window
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("window.open('https://www.bing.com','_blank');");
// Switch to the new window
driver.SwitchTo().Window(driver.WindowHandles[1]);
Console.WriteLine("Title of second window: " + driver.Title);
// Switch back to the first window
driver.SwitchTo().Window(driver.WindowHandles[0]);
Console.WriteLine("Title of first window after switching back: " + driver.Title);
// Close the second window
driver.Close();
// Close the first window
driver.Quit();
}
}
}
This code opens the Google homepage in the first window and then opens a new window with the Bing homepage. The code then switches to the new window and prints its title, before switching back to the first window and printing its title again. Finally, the code closes both windows.
Write it in UnitTest instead of Main method
Here's an example of opening two windows using NUnit as the unit testing framework:
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
[TestFixture]
public class MyTests
{
private IWebDriver driver;
[SetUp]
public void Setup()
{
driver = new ChromeDriver();
driver.Manage().Window.Maximize();
}
[Test]
public void TestOpeningMultipleWindows()
{
// Open the first window
driver.Navigate().GoToUrl("https://www.google.com");
Console.WriteLine($"Window Handle 1: {driver.CurrentWindowHandle}");
// Open a new window
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("window.open();");
Console.WriteLine($"Window Handles 2: {string.Join(", ", driver.WindowHandles)}");
// Switch to the new window
driver.SwitchTo().Window(driver.WindowHandles[1]);
driver.Navigate().GoToUrl("https://www.github.com");
Console.WriteLine($"Window Handle 2: {driver.CurrentWindowHandle}");
// Switch back to the first window
driver.SwitchTo().Window(driver.WindowHandles[0]);
Console.WriteLine($"Window Handle 1: {driver.CurrentWindowHandle}");
}
[TearDown]
public void TearDown()
{
driver.Quit();
}
}
This code uses the ChromeDriver to open two windows. The first window is opened by navigating to Google, and the second window is opened using a JavaScript executor. The code then switches to the new window, navigates to GitHub, and then switches back to the original window.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2019-02-15 Package Manager Console的使用
2019-02-15 visual studio中添加existing web site, website名字附带数字
2019-02-15 web platform installer
2017-02-15 .net中的目录