(Download online)
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:
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 (Stri Ng[] 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, output the text above the dialog box, and then fork off the dr.findelement ("Alert"). Click ("By.id"); Alert alert = Dr.switchto (). alert (); String text = Alert.gettext (); System.out.println (text); Alert.dismiss (); Click the second button, output the text above the dialog box, then clickConfirm 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 actions are: getText () gets its text value accept () is equivalent to clicking on its "Confirm" dismiss () equivalent to clicking "Cancel" or cross-out the dialog box SendKeys () input value, this alert\confirm no dialog box can not be used, or will be an error.
Learning Links: http://blog.csdn.net/gzh0222/article/details/7568651
Selenium webdriver----How to handle alert, confirm, prompt dialog boxes