Selenium 简明教程
Selenium WebDriver - Windows and Tabs
Selenium Webdriver 可用于处理窗口和选项卡。Selenium Webdriver 在处理窗口和选项卡时没有区别。每个打开的窗口都有该会话的唯一标识符。
Selenium Webdriver can be used to handle windows and tabs. There is no difference in handling windows and tabs by Selenium Webdriver. Each window opened has a unique identifier for that session.
一旦打开一个新窗口,驱动程序上下文仍然保留在父窗口中。为了对子窗口执行某些任务,我们需要将驱动程序上下文从主窗口切换到子窗口。
Once a new window is opened, the driver context still remains on the parent window. In order to perform some tasks on the child windows, we would need to switch the driver context from the main window to the child windows.
Basic Methods to Handle Windows and Tabs in Selenium
Selenium 中有多种方法可用于基于窗口和选项卡自动执行测试。为了访问子窗口,我们必须首先使用 switchTo().window("子窗口的窗口处理 ID") 方法将驱动程序上下文切换到另一个窗口。处理窗口和选项卡的方法列在下面:
There are multiple methods available in Selenium that can enable us to automate tests based on windows and tabs. For accessing the child windows, we have to first switch the driver context to the other window using the switchTo().window("window handle id of the child window") method. The methods to handle windows and tabs are listed below −
-
driver.getWindowHandle() − This is used to get the window handle id of the window in focus. Its return type is a String.
-
driver.getWindowHandles() − This is used to get all the window handle ids of the windows which are opened. Its return type is a Set of Strings.
Example 1
让我们以下面的页面为例,我们将在其中单击 New Tab 按钮。
Let us take an example of the below page, where we would click on the New Tab button.

单击 New Tab 后,我们将导航到另一个具有文本 New Tab 的选项卡。
After clicking on the New Tab, we would navigate to another tab having the text New Tab.

代码实现
Code Implementation
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public class Tabs {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new ChromeDriver();
// adding implicit wait of 12 secs
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
// Opening the webpage where we will open a new tab
driver.get("https://www.tutorialspoint.com/selenium/practice/browser-windows.php");
// click link and navigate to next tab
WebElement b = driver.findElement
(By.xpath("/html/body/main/div/div/div[2]/button[1]"));
b.click();
// Get the window handle of the original window
String oW = driver.getWindowHandle();
// get all opened windows handle ids
Set<String> windows = driver.getWindowHandles();
// Iterating through all window handles
for (String w : windows) {
if(!oW.equalsIgnoreCase(w)) {
// switching to child tab
driver.switchTo().window(w);
// accessing element in new tab
WebElement e = driver.findElement
(By.xpath("/html/body/main/div/div/h1"));
System.out.println("Text in new tab is: " + e.getText());
break;
}
}
// quitting the browser
driver.quit();
}
}
Text in new tab is: New Tab
Process finished with exit code 0
在上面的示例中,我们在新选项卡中捕获了文本,并在控制台中收到了消息 - Text in new tab is: New Tab 。
In the above example, we captured the text on the new tab and received the message in the console - Text in new tab is: New Tab.
最后,收到了消息 Process finished with exit code 0 ,表示代码成功执行。
Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.
Example 2
让我们以以下图像所示的示例为另一个示例,其中我们将单击 New Window Message 按钮。
Let us take another example as shown in the below image where we would click on the New Window Message button.

在点击 New Window Message 后,我们将导航到另一个窗口,该窗口包含文本 New Window Message 。
After clicking on the New Window Message, we would navigate to another window having the text New Window Message.

然后,我们将关闭新窗口并切换回原始窗口,并在那里获取文本 - Browser Windows 。最后,我们将退出会话。
Then we would close the new window and switch back to the original window and get the text - Browser Windows there. Finally we would quit the session.

代码实现
Code Implementation
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public class Windows {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new ChromeDriver();
// adding implicit wait of 12 secs
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
// Opening the webpage where we will open a new window
driver.get("https://www.tutorialspoint.com/selenium/practice/browser-windows.php");
// click link and navigate to next window
WebElement b = driver.findElement
(By.xpath("/html/body/main/div/div/div[2]/button[3]"));
b.click();
// Get the window handle of the original window
String oW = driver.getWindowHandle();
// get all opened windows handle ids
Set<String> windows = driver.getWindowHandles();
// Iterating through all window handles
for (String w : windows) {
if(!oW.equalsIgnoreCase(w)) {
// switching to child window
driver.switchTo().window(w);
// accessing element in new window
WebElement e = driver.findElement
(By.xpath("/html/body/main/div/div/h1"));
System.out.println("Text in new window is: " + e.getText());
driver.close();
break;
}
}
// switching to parent window
driver.switchTo().window(oW);
// accessing element in parent window
WebElement e1 = driver.findElement
(By.xpath("/html/body/main/div/div/div[2]/h1"));
System.out.println("Text in parent window is: " + e1.getText());
// quitting the browser session
driver.quit();
}
}
Text in new window is: New Window Message
Text in parent window is: Browser Windows
在上面的示例中,我们在新窗口中捕获文本并在控制台中收到消息 - Text in new window is: New Window Message 。然后,我们关闭子窗口并切换回父窗口。最后,在控制台中获取父窗口上的文本 - Text in parent window is: Browser Windows 。
In the above example, we captured the text on the new window and received the message in the console - Text in new window is: New Window Message. Then we closed the child window and switched back to the parent window. Lastly, obtain the text on the parent window in the console - Text in parent window is: Browser Windows.
Difference between close() and quit() methods
close() 方法和 quit() 方法之间是有区别的。虽然 close() 方法仅关闭焦点所在的浏览器窗口,但 quit() 方法关闭与驱动程序会话(浏览器和所有后台驱动程序进程)相关的所有内容。
There is a difference between close() and quit() methods. While the close() method only closes the browser window in focus, the quit() method closes everything associated with the driver session(browser and all background driver processes).
Example 1 - Open New Window with Selenium 4
Selenium 4 提供了打开新标签页或窗口的选项。要打开新窗口,我们将使用该方法的帮助 −
Selenium 4 gives the option to open a new tab or window. To open a new window, we would take the help of the method −
driver.switchTo().newWindow(WindowType.WINDOW)
让我们举另一个示例,我们首先会打开一个窗口并启动一个带有文本 - Check Box 的应用程序。
Let us take another example where we would first open a window and launch an application having the text - Check Box.

然后,我们将打开一个新窗口并启动另一个带有文本 - Radio Button 的应用程序。
Then we would open a new window and launch another application having the text - Radio Button.

代码实现
Code Implementation
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class NewWindow {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new ChromeDriver();
// adding implicit wait of 12 secs
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
// Opening the webpage in window 1
driver.get("https://www.tutorialspoint.com/selenium/practice/check-box.php");
// get text in window 1
WebElement e = driver.findElement
(By.xpath("/html/body/main/div/div/div[2]/h1"));
System.out.println("Text is: " + e.getText());
// Initiate the Webdriver
WebDriver newDriver = driver.switchTo().newWindow(WindowType.WINDOW);
// Opening the webpage in new window
driver.get("https://www.tutorialspoint.com/selenium/practice/radio-button.php");
// get text in window 2
WebElement e1 = driver.findElement
(By.xpath("/html/body/main/div/div/div[2]/form/h1"));
System.out.println("Text in new window is: " + e1.getText());
// quitting the browser session
driver.quit();
}
}
Text is: Check Box
Text in new window is: Radio Button
在上面的示例中,我们在第一个窗口中捕获文本并在控制台中收到消息 - Text is: Check Box 。然后打开另一个窗口和那里的应用程序。最后,我们在控制台的新窗口中获取文本 - Text in new window is: Radio Button 。
In the above example, we captured the text in the first window and received the message in the console - Text is: Check Box. Then opened another window and an application there. Finally, we obtained the text in the new window in the console - Text in new window is: Radio Button.
Example 2 - Open New Tab with Selenium 4
Selenium 4 提供了打开新标签页或窗口的选项。要打开新标签页,我们将使用该方法的帮助 −
Selenium 4 gives the option to open a new tab or window. To open a new tab, we would take the help of the method −
driver.switchTo().newWindow(WindowType.TAB)
让我们举另一个示例,我们首先会打开一个窗口并启动一个带有文本 - Check Box 的应用程序。
Let us take another example where we would first open a window and launch an application having the text - Check Box.

然后,我们将打开一个新标签页并启动另一个带有文本 - Radio Button 的应用程序。
Then we would open a new tab and launch another application having the text - Radio Button.

NewTabs.java 类文件上的代码实现。
Code Implementation on NewTabs.java class file.
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class NewTabs {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new ChromeDriver();
// adding implicit wait of 12 secs
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
// Opening the webpage in window 1
driver.get("https://www.tutorialspoint.com/selenium/practice/check-box.php");
// get text in window 1
WebElement e = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/h1"));
System.out.println("Text is: " + e.getText());
// Initiate the Webdriver
WebDriver newDriver =driver.switchTo().newWindow(WindowType.TAB);
// Opening the webpage in new tab
driver.get("https://www.tutorialspoint.com/selenium/practice/radio-button.php");
// get text in other tab
WebElement e1 = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/form/h1"));
System.out.println("Text in other tab is: " + e1.getText());
// quitting the browser session
driver.quit();
}
}
Text is: Check Box
Text in other tab is: Radio Button
在上面的示例中,我们在第一个窗口中捕获文本并在控制台中收到消息 - Text is: Check Box 。然后打开另一个标签页和那里的应用程序。最后,我们在控制台的新标签页中获取文本 - Text in other tab is: Radio Button 。
In the above example, we captured the text in the first window and received the message in the console - Text is: Check Box. Then opened another tab and an application there. Finally, we obtained the text in the new tab in the console - Text in other tab is: Radio Button.
Conclusion
这结束了我们对 Selenium WebDriver Windows 和 Tabs 教程的全面介绍。我们从描述处理 Selenium 中的窗口和标签的基本方法开始,介绍了如何使用 Selenium Webdriver 处理窗口和标签的示例,以及 Selenium 中 close() 和 quit() 方法之间的区别。这使您深入了解 Selenium WebDriver - Windows 和 Tabs。明智的做法是继续实践您所学到的内容并探索其他与 Selenium 相关的知识,以加深您的理解并拓展您的视野。
This concludes our comprehensive take on the tutorial on Selenium WebDriver Windows and Tabs. We’ve started with describing basic methods to handle windows and tabs in Selenium, walked through examples on how to handle windows and tabs with Selenium Webdriver, and what is the difference between close() and quit() methods in Selenium. This equips you with in-depth knowledge of the Selenium WebDriver - Windows and Tabs. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.