WEB automation (JAVA version)-positioning and operation of special elements-modal box

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();
	}
}
Published 73 original articles · won praise 2 · Views 3154

Guess you like

Origin blog.csdn.net/anniewhite/article/details/105343283