Strategy |
Grammar |
Grammar |
Description |
by ID |
Driver.findelement (By.id ()) |
Driver.findelementS(By.id ()) |
Locating elements by id attribute |
By name |
Driver.findelement (By.name ()) |
Driver.findelementS(By.name ()) |
Locating elements through the Name property |
By class name |
Driver.findelement (By.classname ()) |
Driver.findelementS(By.classname ()) |
Locating elements through the class attribute |
by tag name |
Driver.findelement (By.tagname ()) |
Driver.findelementS(By.tagname ()) |
Positioning elements with HTML tag names |
By link text |
Driver.findelement (By.linktext ()) |
Driver.findelementS(By.linktext ()) |
Positioning elements by linking content |
by partial link text |
Driver.findelement (By.partiallinktext ()) |
Driver.findelementS(By.partiallinktext ()) |
Locating elements through partially linked content |
by CSS |
Driver.findelement (By.cssselector ()) |
Driver.findelementS(By.cssselector ()) |
Positioning elements with CSS selectors |
by XPath |
Driver.findelement (By.xpath ()) |
Driver.findelementS(By.xpath ()) |
Positioning elements through XPath |
Findelement:
1. By ID
Using the Id,name,class property is the preferred method for locating elements. Where the ID of the element is the most preferred method, is the fastest strategy.
The id attribute cannot be used when the following conditions occur:
- Not all page elements have an id attribute
- The value of the id attribute is dynamically generated
2. Findelement () method locates an element, queries the entire DOM, and then returns the first matching element
3. The Webelement class can support querying child class elements. Assume that there are some duplicate elements on the page that have different parent elements. We can locate its parent element first, and then locate its child elements, as follows:
Webelement father = Driver.findelement (By.id ("father"= driver.findelement (by.linktext ("xxx"));
They can also be abbreviated as a line:
1 webelement son = driver.findelement (By.id ("Father")). Findelement (Bylinktext ("xxx"));
4. Nosuchelementfoundexception
The Findelement () and findelements () methods throw the exception when the corresponding element is not found.
Findelements:
Selenium Webdriver's findelementS() method allows you to get a collection of specified rules for situations where you need to operate on a similar set of elements.
Example: Verify the number of Baidu home navigation links and print out their hyperlink address
Implementation code:
1 Importjava.util.List;2 3 Importorg.openqa.selenium.By;4 ImportOrg.openqa.selenium.WebDriver;5 Importorg.openqa.selenium.WebElement;6 ImportOrg.openqa.selenium.chrome.ChromeDriver;7 Importorg.testng.annotations.Test;8 9 ImportCom.thoughtworks.selenium.SeleneseTestNgHelper;Ten One Public classfindelements { A - @Test - Public voidTest () { theSystem.setproperty ("Webdriver.chrome.driver", "Chromedriver.exe"); -Webdriver Driver =Newchromedriver (); -Driver.get ("http://www.baidu.com"); -List<webelement> links = driver.findelements (By.cssselector ("#u1 a")); + //gets the total number of elements in the collection and goes to string format (below Assertequals method parameter format requirement) -String size =string.valueof (Links.size ()); + //String size = integer.tostring (Links.size ()); A //String size = links.size () + ""; at //Verify the number of links -Selenesetestnghelper.assertequals ("8", size); - - //Print href attribute - //get all the elements in the list through a for loop, and then call the GetAttribute () method to get the attributes of the element - for(inti=0; I<links.size (); i++){ inSystem.out.println (Links.get (i). getattribute ("href")); - } to driver.quit (); + } -}
Operation Result:
[Selenium Webdriver Java] element positioning--findelement/findelements