特殊元素定位與操作-模態框
Modal Dialogue Box,又叫做模式對話框,是指在用戶想要對對話框以外的應用程序進行操作時,必須首先對該對話框進行響應。如單擊【確定】或【取消】按鈕等將該對話框關閉。
- alert
- confirm
代碼示例
alert代碼示例
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代碼示例
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();
}
}