我正在尝试学习selenium并使用firebug来检查页面上的元素。但是,每当有任何弹出或警告消息时,我都无法检查它,因为它不允许我单击检查元素而不在弹出窗口上单击“确定”。 我需要自动点击“确定”按钮,但无法识别对象属性。
答案 0 :(得分:2)
实际上,您无法对警报执行检查元素,因此您可以通过以下方式处理这些警报:
//WAIT UNTIL ALERT OPENS AND THEN CLICK ON OK/ACCEPT
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept(); //ACCEPT ACTION
//WAIT UNTIL ALERT OPENS AND THEN CLICK ON CANCEL/REJECT
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.dismiss(); //REJECT ACTION
上面我正在使用显式等待,等待警报直到30秒。然后我切换到警告框,最后只是接受/拒绝该警报行动。
答案 1 :(得分:1)
你无法检查brwoser警报,我为handel浏览器警报做了一些可爱的课程 像这样
import org.openqa.selenium.Alert;
import org.openqa.selenium.UnhandledAlertException;
import org.openqa.selenium.WebDriver;
/**
*
* this class for all methods we are used to handle browser alert.
*/
public class BrowserAlertHelper {
// this is method to check if there any browser alert.
public static boolean isDialogPresent(WebDriver driver) {
try {
driver.getTitle();
return false;
} catch (UnhandledAlertException e) {
// Modal dialog showed
return true;
}
}
//this is method to accept browser alert.
public static void acceptBrowserAlert(WebDriver driver) {
Alert alert = driver.switchTo().alert();
alert.accept();
}
//this is method to decline browser alert.
public static void declineBrowserAlert(WebDriver driver) {
Alert alert = driver.switchTo().alert();
alert.dismiss();
}
// get alert text
public static String getBrowserAlertText(WebDriver driver){
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
return alertText;
} catch (Exception e) {
System.out.println("no browser alert showing");
}
return null;
}
}
只需调用所需的方法,并将webdriver作为参数
发送给您