Selenium webdriver----How to handle alert, confirm, prompt dialog boxes

Source: Internet
Author: User
Tags gettext

(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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.