我希望使用selenium关闭我父母窗口上的弹出窗口。我正在使用java。
到目前为止,我正在遍历页面上的所有DIVs,直到看到" close“文本(带有关闭文本的按钮),然后如果我找到close,则单击close按钮,但这需要时间,因为它遍历所有DIVs。
当前代码:
// 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中关闭弹出窗口。
发布于 2018-06-01 04:16:38
如果您希望接受警报弹出窗口并根据需要传递超时,请尝试此方法(示例: 4000 (4秒))
public static void acceptAlertIfAvailable(long timeout) {
//log.info("Checking if there is a popup alert to accept it");
long waitForAlert = System.currentTimeMillis() + timeout;
boolean boolFound = false;
do {
try {
Alert alert = driver.switchTo().alert();
if (alert != null) {
String alertMsg = alert.getText();
alert.accept();
//log.info("Pop-up alert [" + alertMsg + "] was found and accepted");
boolFound = true;
}
} catch (NoAlertPresentException ex) {
//log.error("Pop-up alert Not handled and Exception occured --> " + ex.getMessage());
}
} while ((System.currentTimeMillis() < waitForAlert) && (!boolFound));
}
如果您想要解除警报,请尝试此方法:
public static void dismissAlertIfAvailable(long timeout) {
//log.info("Checking if there is a popup alert to dismiss it");
long waitForAlert = System.currentTimeMillis() + timeout;
boolean boolFound = false;
do {
try {
Alert alert = driver.switchTo().alert();
if (alert != null) {
String alertMsg = alert.getText();
alert.dismiss();
//log.info("Pop-up alert [" + alertMsg + "] was found and dismissed");
boolFound = true;
}
} catch (NoAlertPresentException ex) {
//log.error("Pop-up alert Not handled and Exception occured --> " + ex.getMessage());
}
} while ((System.currentTimeMillis() < waitForAlert) && (!boolFound));
}
https://stackoverflow.com/questions/50624770
复制相似问题