Locating elements in Selenium WebDriver is performed with the help of findElement() and findElements() methods provided by WebDriver and WebElement class.
findElement() returns a WebElement object based on a specified search criteria or ends up throwing an exception if it does not find any element matching the search criteria.
findElements() returns a list of WebElements matching the search criteria. If no elements are found, it returns an empty list.
The following table lists all the Java syntax for locating elements in Selenium WebDriver.
Method | Syntax | Description |
---|---|---|
By ID | driver.findElement(By.id (<element ID>)) | Locates an element using the ID attribute |
By name | driver.findElement(By.name (<element name>)) | Locates an element using the Name attribute |
By class name | driver.findElement(By.className (<element class>)) | Locates an element using the Class attribute |
By tag name | driver.findElement(By.tagName (<htmltagname>)) | Locates an element using the HTML tag |
By link text | driver.findElement(By.linkText (<linktext>)) | Locates a link using link text |
By partial link text | driver.findElement(By.partialLinkText (<linktext>)) | Locates a link using the link's partial text |
By CSS | driver.findElement(By.cssSelector (<css selector>)) | Locates an element using the CSS selector |
By XPath | driver.findElement(By.xpath (<xpath>)) | Locates an element using XPath query |
Now let us understand the practical usage of each of the locator methods with the help of https://www.calculator.net
Here an object is accessed with the help of IDs. In this case, it is the ID of the text box. Values are entered into the text box using the sendkeys method with the help of ID(cdensity).
driver.findElement(By.id("cdensity")).sendKeys("10");
Here an object is accessed with the help of names. In this case, it is the name of the text box. Values are entered into the text box using the sendkeys method with the help of ID(cdensity).
driver.findElement(By.name("cdensity")).sendKeys("10");
Here an object is accessed with the help of Class Names. In this case, it is the Class name of the WebElement. The Value can be accessed with the help of the gettext method.
List<WebElement> byclass = driver.findElements(By.className("smalltext smtb"));
The DOM Tag Name of an element can be used to locate that particular element in the WebDriver. It is very easy to handle tables with the help of this method. Take a look at the following code.
WebElement table = driver.findElement(By.id("calctable")); List<WebElement> row = table.findElements(By.tagName("tr")); int rowcount = row.size();
This method helps to locate a link element with matching visible text.
driver.findElements(By.linkText("Volume")).click();
This methods helps locate a link element with partial matching visible text.
driver.findElement(By.partialLinkText("Volume")).click();
The CSS is used as a method to identify the webobject, however NOT all browsers support CSS identification.
WebElement loginButton = driver.findElement(By.cssSelector("input.login"));
XPath stands for XML path language. It is a query language for selecting nodes from an XML document. XPath is based on the tree representation of XML documents and provides the ability to navigate around the tree by selecting nodes using a variety of criteria.
driver.findElement(By.xpath(".//*[@id = 'content']/table[1]/tbody/tr/td/table/tbody/tr[2]/td[1]/input")).sendkeys("100");