【发布时间】:2018-06-01 04:16:37
【问题描述】:
我希望使用 selenium 关闭父窗口上的弹出窗口。我正在使用 java。
到目前为止,我正在循环浏览页面上的所有 DIV,直到我看到“关闭”文本(带有关闭文本的按钮),然后如果我找到关闭,然后单击关闭按钮但是这需要时间,因为它会遍历所有 DIV .
当前代码:
// Find the visible element that has text 'Close' and click on it
WebElement closeButton = Functional
.getVisibleElement(pDriver.findElements(By.xpath("//span[contains(.,'Close')]")));
if (closeButton != null) {
closeButton.click();
public static WebElement getVisibleElement(List<WebElement> pListOfElements) {
for (int i = 0; i < pListOfElements.size(); i++) {
if (pListOfElements.get(i).isDisplayed()) {
for (int a = 0; a <= ATTEMPT; a++) {
try {
return pListOfElements.get(i);
} catch (StaleElementReferenceException e) {
LOGGER.info("attempting to press the element. Amount of attempt: " + ATTEMPT);
}
}
} else {
}
}
return null;
}
有什么方法可以关闭 selenium 中的弹出窗口。
【问题讨论】:
-
看起来像是一个 javascript 弹出窗口而不是一个新窗口?如果一次只有一个弹出窗口,也许有一个更容易找到的定位器。你能添加相关的html吗?
-
不是警报之类的 java 脚本窗口。它是来自服务器的响应,显示在窗口中。