Selenium Web Automation-how to find elements
2016-07-29
1. What is an element?
Element: http://www.w3school.com.cn/html/html_elements.asp
2. Positioning method Analysis
Selenium Webdriver provides an advanced technology to locate Web page elements. The Selenium feature-rich API provides multiple targeting strategies such as Name, ID, CSS selector, XPath, and so on, as shown in:
It is usually used to locate the ID, because it is unique, XPath is also more common, Firefox plugin: Firepath and Firebug are quick to give the XPath of the element.
In addition, jquery is a good way to locate
2.1 Positioning elements with jquery
jquery provides a Find method to quickly find page elements, but this approach is limited to pages that use the jquery library.
2.1.1 How to differentiate the page using jquery
Open Firefox, enter Google Address: https://www.google.com.hk/, press F12 to bring up the console, then enter at the bottom of the arrow: "JQuery", and then enter:
- If the console returns: "function (E, T)" This indicates that this page applies to the jquery library.
- If the console returns an error message: Referenceerror:jquery is not defined this proves that the current page does not apply to the JQuery library
The code is as follows:
1 Packagewaytofindelement;2 3 Importjava.util.List;4 ImportOrg.openqa.selenium.JavascriptExecutor;5 ImportOrg.openqa.selenium.WebDriver;6 Importorg.openqa.selenium.WebElement;7 ImportOrg.openqa.selenium.firefox.FirefoxDriver;8 ImportOrg.testng.Assert;9 Importorg.openqa.selenium.WebDriverException;Ten One Public classFindbyjquery { A - Public Static voidMain (string[] args) { - Testnotloadjquery (); the } - - Private Static voidTestloadedjquery () { -Webdriver Driver =Newfirefoxdriver (); + driver.manage (). window (). Maximize (); -Javascriptexecutor Jsexecutor =(javascriptexecutor) driver; +Driver.get ("https://www.baidu.com"); A atList<webelement> webelements = (list<webelement>) jsexecutor.executescript ("Return JQuery.find (' A.mnav ') )"); -Assert.assertequals (Webelements.size (), 6); -Assert.assertequals (Webelements.get (2). GetText (), "hao123"); - driver.quit (); - } - in Private Static voidtestnotloadjquery () - { toWebdriver Driver =Newfirefoxdriver (); + driver.manage (). window (). Maximize (); -Javascriptexecutor Jsexecutor =(javascriptexecutor) driver; theDriver.get ("http://www.2345.com/"); * if(!jqueryloaded (Jsexecutor)) { $ //If no jquery library is detected, the injection operation is performedPanax Notoginseng injectjquery (jsexecutor); - } the //Find a Search button element +List<webelement> SearchButton = (list<webelement>) jsexecutor.executescript ("Return JQuery.find (' Input.sch_btn ') "); A //Verify the text of the button theAssert.assertequals (Searchbutton.get (0). getattribute ("value"), "search"); + driver.quit (); - } $ $ /**inject jquery support*/ - Public Static voidinjectjquery (Javascriptexecutor jsexecutor) { -Jsexecutor.executescript ("var headid = document.getElementsByTagName (\" head\ ") [0];" the+ "var newscript = document.createelement (' script ');" -+ "Newscript.type = ' text/javascript ';"Wuyi+ "newscript.src=\" http://code.jquery.com/jquery-2.1.4.min.js\ ";" the+ "Headid.appendchild (newscript);"); - } Wu - About /**determine if the current page uses jquery*/ $ Public StaticBoolean jqueryloaded (javascriptexecutor jsexecutor) { -Boolean loaded =true; - Try{ -Loaded = (Boolean) jsexecutor.executescript ("Return jQuery ()!" = NULL "); A}Catch(webdriverexception e) { +Loaded =false; the } - $ returnloaded; the } the}
View Code
Selenium Web Automation-how to find elements