1. Alert Class
- Alert refers to some of the Windows popup operations and requires a new alert class
- Driver.switchto (). Alert (): Switch to alert window
- Alert.gettext (): Gets the string above the popup window
- Alert.accept (): Click OK/ok class button to make the pop-up window disappear
- 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
- Start with a new actions class object
- 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
- Start with a new actions class object
- 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
- Usually used to execute a section of JS, to change the HTML
- Some non-standard controls cannot use the Selenium2 API, you can execute JS method to replace
- 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
- 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");
- 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);
- You can also locate an IFRAME by XPath, using the following syntax:
- Webelement iframe = driver.findelement (By.xpath ("//iframe[@name = ' AA ')");
- 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
- Gettwindowhandles: Gets the handle of all pages opened by driver
- 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)