Selenium Webdriver Learning (vii)------------How to handle alert, confirm, prompt dialog box (GO)

Source: Internet
Author: User
Tags gettext

Selenium Webdriver Learning (vii)------------How to handle alert, confirm, prompt dialog boxes Blog Category:
    • Selenium-webdriver
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
    1. Dialogs.html
HTML code
  1. <html>
  2. <head>
  3. <title>alert</title>
  4. </head>
  5. <body>
  6.         <input < span class= "attribute" >id =  "alert"  value  =  "alert"  type = " button " onclick = " Alert (' Welcome! Please press confirm to continue! ‘);" />  
  7.     <input id  =  "confirm"  value =  "confirm"  type =  " Button " onclick = " confirm (' OK? ‘);" />  
  8. <input id = "prompt" value = "Prompt" type = " button" onclick = "var name = prompt (' Please enter your name: ', ' Please enter
  9. Your name '); document.write (name) "/>
  10. </Body>
  11. </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
  1. Import Org.openqa.selenium.Alert;
  2. Import Org.openqa.selenium.By;
  3. Import Org.openqa.selenium.WebDriver;
  4. Import Org.openqa.selenium.firefox.FirefoxDriver;
  5. Public class Dialogsstudy {
  6. /** 
  7. * @author GONGJF
  8. */
  9. public static void Main (string[] args) {
  10. //TODO auto-generated method stub
  11. System.setproperty ("Webdriver.firefox.bin","D:\\Program files\\mozilla firefox\\firefox.exe");
  12. Webdriver dr = new Firefoxdriver ();
  13. String url = "file:///C:/Documents and settings/gongjf/desktop/selenium_test/dialogs.html";// "/your/path/to/ Main.html "
  14. Dr.get (URL);
  15. //Click the first button to output the text above the dialog box and then fork out
  16. Dr.findelement (By.id ("alert")). Click ();
  17. Alert alert = Dr.switchto (). alert ();
  18. String text = Alert.gettext ();
  19. System.out.println (text);
  20. Alert.dismiss ();
  21. //Click the second button to output the text above the dialog box and click OK
  22. Dr.findelement (By.id ("confirm")). Click ();
  23. Alert confirm = Dr.switchto (). alert ();
  24. String Text1 = Confirm.gettext ();
  25. System.out.println (Text1);
  26. Confirm.accept ();
  27. //Click the third button, enter your name, then click OK, and finally
  28. Dr.findelement (By.id ("prompt")). Click ();
  29. Alert prompt = Dr.switchto (). alert ();
  30. String Text2 = Prompt.gettext ();
  31. System.out.println (TEXT2);
  32. Prompt.sendkeys ("Jarvi");
  33. Prompt.accept ();
  34. }
  35. }

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)

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.