Original link: http://www.cnblogs.com/itliucheng/p/5578788.html
Some API
1. Visit the website
Driver.get ("http://www.baidu.com");
Or
Driver.navigate (). to ("http://www.baidu.com");
2. Manipulating the browser window
new Point; // declares a dimension object, two 500 indicates the length and width of the browser window New Dimension (a); // set the size of the browser window to 500 wide driver.manage (). window (). setSize (dimension); // maximizes browser driver.manage (). window (). Maximize ();
3. Browser input values and clicks
Driver.navigate (). to ("http://www.baidu.com"), Driver.findelement (By.id ("kw")). Clear (); Driver.findelement (By.id ("kw")). SendKeys ("Warcraft");d river.findelement (by.id ("su")). Click ();
4. Select list to operate multiple selections
Select Dropselect =NewSelect (Driver.findelement (By.name ("Fruit")));//determine if the page is multi-selectedassert.asserttrue (Dropselect.ismultiple ());//Select the third option using the selection indexDropselect.selectbyindex (3); //select based on value valuesDropselect.selectbyvalue ("Value"); //select based on option textDropselect.selectbyvisibletext ("Apple"); //Uncheck all optionsDropselect.deselectall ();//cancels the third selected stateDropselect.deselectbyindex (3); //deselect based on value valuesDropselect.deselectbyvalue ("Value"); //deselect based on option textDropselect.deselectbyvisibletext ("Apple");
5. Operate the Radio box
Webelement radiooption = driver.findelement (By.xpath ("//input[@value = ' Orange ']")); if(!radiooption.isselected ()) {Radiooption.click (); } List<WebElement> fruits = driver.findelements (by.name ("Fruit")); for(webelement fruit:fruits) {if(Fruit.getattribute ("value"). Equals ("Watermelon")){ if(!fruit.isselected ()) {Fruit.click (); Assert.asserttrue (fruit.isselected ()); Break; } } }
6. check box Ibid.
7. Executing JavaScript scripts
Javascriptexecutor js = (javascriptexecutor) driver; // Cancel upload input hidden js.executescript ("document.getElementById (\" File\ "). Style=\" Display:block;\ "");
Javascriptexecutor js = (javascriptexecutor) driver; // returns the text on the search button String text = (string) js.executescript ("var button = document.getElementById (\" Stb\ "); return Button.value");
8. Wait for operation
Sets the maximum wait time for a page element to be found, without immediately locating an element when calling the Findelement method
, the program will constantly try to determine whether the found element appears in the DOM of the page after a certain period of time , if more than
The set wait time is still not found, then throw nosuchelementexception
Invisible wait
Driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds);
@BeforeClass Public Static voidinit () {System.out.println ("Init ..."); System.setproperty ("Webdriver.chrome.driver", "C:\\Program Files (x86) \\Google\\Chrome\\Application\\chromedriver.exe"); //Create a Chromedriver interface to connect to Chrome,//must have Chromedriver.exe file, selenium cannot start chrome by default//Create a browser instance of ChromeDriver =NewChromedriver (); //Maximize Your browserdriver.manage (). window (). Maximize (); //set the global stealth waitDriver.manage (). Timeouts (). Implicitlywait (10, Timeunit.seconds); }
The default length of the implicit wait is 0, and once set, the implicit wait will work throughout the life cycle of the Webdriver object instance.
Show wait
Show wait is less time than implicit wait to test script execution, it is recommended to use display wait to determine whether the page element exists
The use of the Expectedconditions class in the method, you can display the wait for the judgment
//whether the page elements are available on the page and can be clickedexpectedconditions.elementtobeclickable (by locator);//whether the page element is in the selected stateexpectedconditions.elementtobeselected (by locator);//whether the page element exists on the pageexpectedconditions.presenceofelementlocated (by locator);//whether to include specific textexpectedconditions.texttobepresentinelement (Locator, text)//page element ValuesExpectedconditions.texttobepresentinelementvalue (locator, text);//titleExpectedconditions.titlecontains (title);
// wait for the element to be visible and can be clicked wait.until (Expectedconditions.elementtobeclickable (by.id (ID)));
Custom Display Waits
public static void Sendkeysbyxpath (webdriver driver, string path, string key) {Webdriverwa It wait = new webdriverwait (Driver, 10); // up to 10 seconds webelement element = Wait.until (new Expectedcondition<webelement> () {@Override public Web Element apply (Webdriver d) { return D.findelement (By.xpath (path)); } }); Highlightelement (driver,element); Element.clear (); Element.sendkeys (key); }
Note: If the element is already present in the DOM, but is hidden at this point, you can get the element, but the lack of operation
The procedure is to wait for the thread or wait for the element to be available, recommend the second, the thread waits a variable time, unstable
Public Static voidSendkeysbyxpath (webdriver Driver, string path, string key) {webdriverwait wait=NewWebdriverwait (driver, 10);//wait up to 10 seconds//wait for elements to be visible and can be clickedWait.until (expectedconditions.elementtobeclickable (By.xpath (path)); webelement element= Wait.until (NewExpectedcondition<webelement>() {@Override Publicwebelement Apply (webdriver d) {returnd.findelement (By.xpath (path)); } }); Highlightelement (driver,element); Element.clear (); Element.sendkeys (key); }
9. Action JavaScript Alert pop-up window
driver.findelement (Path). Click (by.xpath); // switch to the alert pop-up box Alert alert = Driver.switchto (). alert (); Assertionutil.assertequals ("This is an alert pop-up box", Alert.gettext ()); // using the alert's Accept method, click the alert's OK button to close the alert alert.accept (); // If alert does not eject, it throws a Noalertpresentexception exception // Switch back to the original form Driver.switchto (). Defaultcontent ();
confirm pop-up window for JavaScript operation
Confirm is similar to alert, unlike
Alert.accept (); Yes, sure.
Alert.dismiss (); Yes, cancel.
11. Operation Frame
Sometimes when we locate a page element, we find that we can't locate it, and check the locator you wrote repeatedly without any problems.
The code does not have any problems. At this point you have to look at whether the page element is in an IFRAME, which may be missing
One of the reasons. If you are looking for an element in the IFRAME in a default content, it must not be found. Instead
You find another IFRAME element in an IFRAME or an element in the default content, and that must not be positioned
// into the frame of id= "frame" or name= "frame",
12. Operation Modal Frame
Driver.switchto (). Activeelement ();
13. Parent-Child Box
String newhandle = "";//gets the current Window parent windowString Currenthandle = Driver.getwindowhandle ();//Click on the page element to open a new windowSet<string> handles = Driver.getwindowhandles ();//get all browser windowsiterator<string> itwin = Handles.iterator (); while(Itwin.hasnext ()) {String key=Itwin.next (); if(Currenthandle.equals (key)) {Continue; } //get a new windowNewhandle =key;}//Toggle WindowWebdriver Newdriver =driver.switchto (). window (newhandle);//Business Operations//remove new window from setHandles.remove (newhandle);//cut back to main windowDriver.switchto (). window (Currenthandle);
14.js Click
Sometimes when you click on the page there will be a situation where the element is covered by other elements, for example, the element is being blocked by a modal box, and the modal box is not gone, it is waiting for
Modal box disappears, click once. or use JS to trigger a click
/*** JS Execution Click event For:element is not clickable at point. Other element would * receive the click * *@paramdriver *@paramselector*/ Public Static voidClickelementbyjs (Webdriver driver, by selector) {webdriverwait wait=NewWebdriverwait (driver, 10);//wait up to 10 secondswebelement element = Wait.until (NewExpectedcondition<webelement>() {@Override Publicwebelement Apply (webdriver d) {returnd.findelement (selector); } }); ((Javascriptexecutor) driver). Executescript ("Arguments[0].click ();", Element); }
[Turn]java Selenium webdriver Practical Application Summary