• Selenium Video Tutorials

Selenium WebDriver - 多窗口测试



Selenium Webdriver 可用于处理多窗口测试。所有启动的窗口都由会话的唯一标识符标识。当另一个窗口打开时,驱动程序的上下文将继续在父窗口上。要在子窗口上执行任务,需要将驱动程序的上下文从父窗口切换到子窗口。

在 Selenium 中处理多个窗口的基本方法

Selenium 中有各种方法可用于自动化处理多个窗口的测试。要在子窗口上工作,需要将驱动程序上下文从父窗口切换到子窗口

示例 1

在下面的页面中,单击新标签页

Selenium Multi Windows 1

单击新标签页后,我们将切换到另一个包含文本新标签页的标签页。

Selenium Multi Windows 2

代码实现

package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; import java.util.Set; import java.util.concurrent.TimeUnit; public class TabsHandling { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new EdgeDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // Opening the webpage with new tab driver.get("https://tutorialspoint.com/selenium/practice/browser-windows.php"); // click link then navigate to next tab WebElement b = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/button[1]")); b.click(); // Get original window handle id String oW = driver.getWindowHandle(); // get every windows handle ids Set<String> windows = driver.getWindowHandles(); // loop through all window handles for (String w : windows) { if(!oW.equalsIgnoreCase(w)) { // switch to the child tab driver.switchTo().window(w); // get element in new tab WebElement e = driver.findElement(By.xpath("/html/body/main/div/div/h1")); System.out.println("Text in new tab: " + e.getText()); break; } } // quit the browser driver.quit(); } }

输出

Text in new tab is: New Tab

Process finished with exit code 0

在这里,我们获取了新打开的标签页上的文本,并在控制台中获得了消息 - 新标签页中的文本:新标签页

最后,收到消息进程已完成,退出代码为 0,表示代码已成功执行。

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

示例 2

在下面的页面中,单击新窗口消息

Selenium Multi Windows 3

单击新窗口消息后,我们将切换到另一个包含文本新窗口消息的窗口。

Selenium Multi Windows 4

关闭新窗口,切换回原始窗口,并在那里获取文本 - 浏览器窗口。最后,退出会话。

Selenium Multi Windows 5

代码实现

package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; import java.util.Set; import java.util.concurrent.TimeUnit; public class WindowsOpen { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new EdgeDriver(); // adding implicit wait of 20 secs driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); // Opening the webpage to open a new window driver.get("https://tutorialspoint.com/selenium/practice/browser-windows.php"); // click link to next window WebElement b = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/button[3]")); b.click(); // Obtain original window handle id String oW = driver.getWindowHandle(); // obtain all opened windows handle ids Set<String> windows = driver.getWindowHandles(); // Loop through all window handles for (String w : windows) { if(!oW.equalsIgnoreCase(w)) { // switch to child window driver.switchTo().window(w); // get element in new window WebElement e = driver.findElement(By.xpath("/html/body/main/div/div/h1")); System.out.println("Text in new window: " + e.getText()); driver.close(); break; } } // switch to parent window driver.switchTo().window(oW); // get 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: " + e1.getText()); // quit browser session driver.quit(); } }

输出

Text in new window: New Window Message
Text in parent window: Browser Windows

在这里,我们捕获了新窗口中的文本,并在控制台中获得了消息 - 新窗口中的文本:新窗口消息。然后,我们结束子窗口并返回到父窗口。最后,我们在控制台中获取了父窗口中的文本 - 父窗口中的文本:浏览器窗口

因此,close() 和 quit() 方法之间存在细微差别。close() 方法仅关闭活动浏览器窗口,而 quit() 方法同时终止所有打开的浏览器窗口。

示例 3

从版本 4 开始,我们可以使用以下方法打开一个新窗口 -

driver.switchTo().newWindow(WindowType.WINDOW)

在浏览器窗口中打开一个应用程序,并获取文本 - 如下图所示的复选框 -

Selenium Multi Windows 6

然后打开另一个新窗口并启动一个包含文本 - 单选按钮的不同应用程序。

Selenium Multi Windows 7

代码实现

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.edge.EdgeDriver; import java.util.concurrent.TimeUnit; public class NewWindowOpen { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new EdgeDriver(); // adding implicit wait of 12 secs driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS); // Open a webpage in first window driver.get("https://tutorialspoint.com/selenium/practice/check-box.php"); // get text in first window WebElement e = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/h1")); System.out.println("Text: " + e.getText()); // Initiate the another Webdriver WebDriver newDriver = driver.switchTo().newWindow(WindowType.WINDOW); // Opening another webpage in second window driver.get("https://tutorialspoint.com/selenium/practice/radio-button.php"); // get text in second window WebElement e1 = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/form/h1")); System.out.println("Text in new window: " + e1.getText()); // quit the browser session driver.quit(); } }

输出

Text: Check Box

Text in new window: Radio Button

在这里,我们捕获了第一个窗口中的文本,并在控制台中获得了消息 - 文本:复选框。然后打开另一个新窗口,并在其中启动一个应用程序。最后,我们在新窗口中获取了文本,并在控制台中获得了一条消息 - 新窗口中的文本:单选按钮

示例 4

从版本 4 开始,我们可以使用以下方法打开一个新标签页 -

driver.switchTo().newWindow(WindowType.TAB)

在浏览器中打开一个应用程序,并获取文本 - 如下图所示的复选框 -

Selenium Multi Windows 8

然后打开另一个新标签页并启动一个包含文本 - 单选按钮的不同应用程序。

Selenium Multi Windows 9

代码实现

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.edge.EdgeDriver; import java.util.concurrent.TimeUnit; public class NewTabsOpen { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new EdgeDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // Open a webpage in first window driver.get("https://tutorialspoint.com/selenium/practice/check-box.php"); // get text in first window WebElement e = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/h1")); System.out.println("Text: " + e.getText()); // Initiate the another Webdriver WebDriver newDriver = driver.switchTo().newWindow(WindowType.TAB); // Open a webpage in new tab driver.get("https://tutorialspoint.com/selenium/practice/radio-button.php"); // obtain 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: " + e1.getText()); // quit the browser session driver.quit(); } }

输出

Text: Check Box
Text in other tab: Radio Button

在这里,我们捕获了第一个窗口中的文本,并在控制台中获得了消息 - 文本:复选框。然后打开另一个新标签页,并在其中启动一个应用程序。最后,我们在新标签页中获取了文本,并在控制台中获得了一条消息 - 其他标签页中的文本:单选按钮

结论

这总结了我们关于 Selenium Webdriver 多窗口测试教程的全面介绍。我们从描述在 Selenium 中处理多个窗口的基本方法开始,并逐步介绍了如何使用 Selenium Webdriver 处理多个窗口的示例。这使您深入了解 Selenium Webdriver - 多窗口测试。明智的做法是不断练习您学到的知识,并探索与 Selenium 相关的其他知识,以加深您的理解并扩展您的视野。

广告