table of Contents
Special element positioning and operation-modal box
Modal Dialogue Box, also known as modal dialog box, means that when the user wants to operate an application other than the dialog box, he must first respond to the dialog box. Click the [OK] or [Cancel] button to close the dialog box.
- alert
- confirm
Code example
alert code example
package com.test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class SpecialElementLocate {
private static ChromeDriver chromeDriver;
public static void main(String[] args) throws InterruptedException {
openChrome();
//访问本地HTML文件
chromeDriver.get("D:\\alert.html");
//点击按钮
chromeDriver.findElement(By.id("abtn")).click();
Thread.sleep(2000);
//switchTo.alert 找到对应的alert弹框
Alert alert = chromeDriver.switchTo().alert();
alert.accept();
//alert.dismiss();
System.out.println(alert.getText());
}
public static void openChrome() {
System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
//1.打开Chrome浏览器
chromeDriver = new ChromeDriver();
}
}
Confirm code example
package com.test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class SpecialElementLocate {
private static ChromeDriver chromeDriver;
public static void main(String[] args) throws InterruptedException {
//confirm弹框处理
//访问本地HTML文件
chromeDriver.get("D:\\confirm.html");
//点击按钮
chromeDriver.findElement(By.id("abtn")).click();
Thread.sleep(2000);
//找到对应的confirm弹框
Alert alert = chromeDriver.switchTo().alert();
//alert.accept();
//alert.dismiss();
System.out.println(alert.getText());
}
public static void openChrome() {
System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
//1.打开Chrome浏览器
chromeDriver = new ChromeDriver();
}
}