【问题标题】:How to open a new window using the new method WindowType of Selenium 4如何使用 Selenium 4 的新方法 WindowType 打开一个新窗口
【发布时间】:2020-11-07 18:21:30
【问题描述】:
Selenium v4.0.0.0-alpha-1 的发行说明提到:
* Added command to open a new window.
源代码:
public static WindowType fromString(String text) {
if (text != null) {
for (WindowType b : WindowType.values()) {
if (text.equalsIgnoreCase(b.text)) {
return b;
}
}
}
return null;
}
}
有人可以帮助我使用 Selenium v4.x 的新方法 WindowType 打开一个标签/窗口吗?
【问题讨论】:
标签:
java
selenium
selenium-webdriver
window-handles
selenium4
【解决方案1】:
在 Selenium 4 中引入了 newWindow(WindowType typeHint) 方法来打开新窗口或标签
/**
* Creates a new browser window and switches the focus for future commands of this driver
* to the new window.
* <p>
* See <a href="https://w3c.github.io/webdriver/#new-window">W3C WebDriver specification</a>
* for more details.
*
* @param typeHint The type of new browser window to be created. The created window is not
* guaranteed to be of the requested type; if the driver does not support
* the requested type, a new browser window will be created of whatever type
* the driver does support.
* @return This driver focused on the given window
*/
WebDriver newWindow(WindowType typeHint);
示例用法
System.setProperty("webdriver.edge.driver","D:\\SomeLocation\\msedgedriver.exe");
WebDriver driver = new EdgeDriver();
driver.get("https://www.google.com");
// This will open the new tab
driver.switchTo().newWindow(WindowType.TAB);
driver.get("http://www.google.com");
// This will open the new window
driver.switchTo().newWindow(WindowType.WINDOW);
driver.get("http://www.google.com");