SELENIUM2 (Webdriver) Summary (v)---element operation Advanced (Common Class)

Source: Internet
Author: User
Tags xpath

1. Alert Class

    1. Alert refers to some of the Windows popup operations and requires a new alert class
    2. Driver.switchto (). Alert (): Switch to alert window
    3. Alert.gettext (): Gets the string above the popup window
    4. Alert.accept (): Click OK/ok class button to make the pop-up window disappear
    5. Alert.dismiss (): Cancel
     Public void Testalert () {        = driver.findelement (By.classname ("alert"));        Element.click ();         = Driver.switchto (). alert ();             = Alert.gettext ();                        Alert.accept ();         // Alert.dismiss ();         System.out.println (text);    }

using the Actions class

    1. Start with a new actions class object
    2. The last perform () must be added, otherwise the execution is unsuccessful
     Public void testalertbyactions () {        = driver.findelement (By.classname ("alert"));         New Actions (driver);        Action.click (Element). Perform ();         = Driver.switchto (). alert ();         = Alert.gettext ();        Alert.accept ();         // Alert.dismiss ();         System.out.println (text);    }

2. Action Class

    1. Start with a new actions class object
    2. The last perform () must be added, otherwise the execution is unsuccessful
     Public void testactions () {        = driver.findelement (By.classname ("Over"));         New Actions (driver);        Action.movetoelement (Element). Perform ();         = Driver.findelement (By.id ("Over")). GetText ();        System.out.println (text);            }

3, call JS

    1. Usually used to execute a section of JS, to change the HTML
    2. Some non-standard controls cannot use the Selenium2 API, you can execute JS method to replace
    3. Executescript The parameter of this method is a string, for a section of JS code
     Public void Testjs () {        = (javascriptexecutor) driver;        Executor.executescript ("alert (' HelloWorld ')");    }

4. Wait mechanism and implementation

    • Returns in the specified time as long as the condition is met, the following code is returned as long as isdisplayed
 Public voidtestwait () {webelement Waitbutton= Driver.findelement (by.id ("Wait"));        Waitbutton.click (); BooleanFlag =NewWebdriverwait (Driver, 10). Until (NewExpectedcondition<boolean>()                         {                             PublicBoolean Apply (webdriver driver) {returnDriver.findelement (By.classname ("Red") . isdisplayed ();        }                             }                ); if(flag) {String text= Driver.findelement (By.classname ("Red") . GetText ();            System.out.println (text); }    }

5. IFRAME Operation

    1. If the IFRAME tag has an ID or name that uniquely identifies it, it can be used directly with the ID or the value of name: Driver.switchto (). FRAME ("AA");
    2. If the IFRAME tag does not have an ID or name, it can be determined on the page that it is the number (that is, by index to locate Iframe,index starting from 0): Driver.switchto (). frame (0);
    3. You can also locate an IFRAME by XPath, using the following syntax:
      1. Webelement iframe = driver.findelement (By.xpath ("//iframe[@name = ' AA ')");
      2. Driver.switchto (). FRAME (iframe);  
     Public void Testiframe () {        driver.findelement("user"). SendKeys ("test");        Driver.switchto (). FRAME ("AA");        Driver.findelement (By.id ("user")). SendKeys ("iframe test");        Driver.switchto (). Defaultcontent (); // return to top-level frame        Driver.findelement (by.id ("User")). SendKeys ("---new test");    

6. Multi-Window switching

    1. Gettwindowhandles: Gets the handle of all pages opened by driver
    2. Witchto is the switch to the appropriate window, and the parameter in window refers to the handle
     Public voidTestmultiwindow () {driver.findelement (By.id ("User"). SendKeys ("Test").); String Handle= Driver.getwindowhandle ();//gets the handle of the current windowSystem.out.println (handle); webelement element= Driver.findelement (By.classname ("open"));        Element.click (); Set<String> handles =Driver.getwindowhandles ();  for(String s:handles) {if(!s.equals (handle))                {System.out.println (s);                Driver.switchto (). window (s); Driver.findelement (By.id ("KW"). SendKeys ("Glen"));        }} driver.switchto (). window (handle); Driver.findelement (By.id ("User"). SendKeys ("---new test")); }

SELENIUM2 (Webdriver) Summary (v)---element operation Advanced (Common Class)

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.