Selenium Webdriver Learning (vii)------------How to handle alert, confirm, prompt dialog boxes
Blog Category:
Alertpromptconfirmseleniumwebdriver
The JS dialog box such as alert, confirm, prompt is SELENIUM1. The X-age is also a hard-to-chew bone, often with AutoIt to help deal with it.
Try it out. Selenium webdriver the dialog boxes are very handy and simple to handle. Take the following HTML code as an example:
HTML code
- Dialogs.html
HTML code
- <html>
- <head>
- <title>alert</title>
- </head>
- <body>
- <input < span class= "attribute" >id = "alert" value = "alert" type = " button " onclick = " Alert (' Welcome! Please press confirm to continue! ‘);" />
- <input id = "confirm" value = "confirm" type = " Button " onclick = " confirm (' OK? ‘);" />
- <input id = "prompt" value = "Prompt" type = " button" onclick = "var name = prompt (' Please enter your name: ', ' Please enter
- Your name '); document.write (name) "/>
- </Body>
- </html>
The above HTML code shows three buttons on the page, click on them to pop up alert, confirm, prompt dialog box respectively. If you enter text in the prompt dialog box and click OK, the page will be refreshed to show the text.
Selenium webdriver The code for handling these shells is as follows:
Java code
- Import Org.openqa.selenium.Alert;
- Import Org.openqa.selenium.By;
- Import Org.openqa.selenium.WebDriver;
- Import Org.openqa.selenium.firefox.FirefoxDriver;
- Public class Dialogsstudy {
- /**
- * @author GONGJF
- */
- public static void Main (string[] args) {
- //TODO auto-generated method stub
- System.setproperty ("Webdriver.firefox.bin","D:\\Program files\\mozilla firefox\\firefox.exe");
- Webdriver dr = new Firefoxdriver ();
- String url = "file:///C:/Documents and settings/gongjf/desktop/selenium_test/dialogs.html";// "/your/path/to/ Main.html "
- Dr.get (URL);
- //Click the first button to output the text above the dialog box and then fork out
- Dr.findelement (By.id ("alert")). Click ();
- Alert alert = Dr.switchto (). alert ();
- String text = Alert.gettext ();
- System.out.println (text);
- Alert.dismiss ();
- //Click the second button to output the text above the dialog box and click OK
- Dr.findelement (By.id ("confirm")). Click ();
- Alert confirm = Dr.switchto (). alert ();
- String Text1 = Confirm.gettext ();
- System.out.println (Text1);
- Confirm.accept ();
- //Click the third button, enter your name, then click OK, and finally
- Dr.findelement (By.id ("prompt")). Click ();
- Alert prompt = Dr.switchto (). alert ();
- String Text2 = Prompt.gettext ();
- System.out.println (TEXT2);
- Prompt.sendkeys ("Jarvi");
- Prompt.accept ();
- }
- }
From the above code can be seen Dr.switchto (). alert (); This sentence can get the object of the Alert\confirm\prompt dialog box, and then use its method to manipulate it. The main methods of dialog operations are:
- GetText () gets its text value
- Accept () is equivalent to clicking on its "Confirm"
- Dismiss () is equivalent to clicking "Cancel" or cross-off the dialog box
- SendKeys () input value, this alert\confirm no dialog box can not be used, or will be an error.
Selenium Webdriver Learning (vii)------------How to handle alert, confirm, prompt dialog box (GO)