Web developers usually need to use JavaScript pop-up dialog boxes to give users some information prompts, including the following types
Reading Contents
Dialog Box Type
1. Warning box: Used to prompt the user for verification results, errors or warnings, etc.
2. Prompt box: Used to prompt the user to enter data in the current dialog box. Generally, the user needs to click the Cancel or Confirm button.
3. Confirm box: Used to prompt the user to confirm or cancel an operation. Generally, the user needs to click the Cancel or Confirm button.
Test page
Use the following page as an example to explain, including warning box, prompt box, and confirm box
http://sislands.com/coin70/week1/dialogbox.htm
Selenium Operation Dialog Code
public static void testAlert(WebDriver driver) { String url="http://sislands.com/coin70/week1/dialogbox.htm"; driver.get(url); WebElement alertButton = driver.findElement(By.xpath("//input[@value='alert']")); alertButton.click(); Alert javascriptAlert = driver.switchTo().alert(); System.out.println(javascriptAlert.getText()); javascriptAlert.accept(); } public static void testPrompt(WebDriver driver) throws Exception { String url="http://sislands.com/coin70/week1/dialogbox.htm"; driver.get(url); WebElement promptButton = driver.findElement(By.xpath("//input[@value='prompt']")); promptButton.click(); Thread.sleep(2000); Alert javascriptPrompt = driver.switchTo().alert(); javascriptPrompt.sendKeys("This is learning Selenium"); javascriptPrompt.accept(); System.out.println(javascriptPrompt.getText()); javascriptPrompt=driver.switchTo().alert(); javascriptPrompt.accept(); Thread.sleep(2000); promptButton.click(); javascriptPrompt=driver.switchTo().alert(); javascriptPrompt.dismiss(); Thread.sleep(2000); javascriptPrompt=driver.switchTo().alert(); javascriptPrompt.accept(); } public static void testConfirm(WebDriver driver) throws Exception { String url="http://sislands.com/coin70/week1/dialogbox.htm"; driver.get(url); WebElement confirmButton = driver.findElement(By.xpath("//input[@value='confirm']")); confirmButton.click(); Thread.sleep(2000); Alert javascriptConfirm = driver.switchTo().alert(); javascriptConfirm.accept(); Thread.sleep(2000); javascriptConfirm = driver.switchTo().alert(); javascriptConfirm.accept(); }The above is the information compilation of the pop-up dialog box for java selenium operation. We will continue to add it later. Thank you for your support for this website!