我尝试使用selenium和java来点击链接或按钮(如下面的html所示)并断言标签或窗口的数量增加,然后只关闭新的标签/窗口。
<div id="req7">
<h2>Test #7</h2>
<button onclick="window.open('');" name="button">Open New Window</button>
<br>
<a target="_blank" href="about:blank" name="newTab">Open New Tab</a>
</div>
我如何用Java解决这个问题?谢谢!
答案 0 :(得分:0)
点击按钮Selenium
// driver can be chrome or something
WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.name("button")));
myDynamicElement.click();
如果您想检查是否有新标签或窗口打开,您应该获得句柄的长度。
//---- before click -----
int initalHandleCount = driver.getWindowHandles().size();
// ---- after click -----
boolean hasNewPage = initalHandleCount < driver.getWindowHandles().size();