SELENIUM - LOCATORS
http://www.tutorialspoint.com/selenium/selenium_locators.htm Copyright © tutorialspoint.com
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.findElementBy. id( < elementID > ) Locates an element using the ID
attribute
By name driver.findElement Locates an element using the Name
By. name( < elementname > ) attribute
By class name driver.findElement Locates an element using the Class
By. className( < elementclass > ) attribute
By tag name driver.findElement Locates an element using the HTML
By. tagName( < htmltagname > ) tag
By link text driver.findElement Locates a link using link text
By. linkText( < linktext > )
By partial link driver.findElement Locates a link using the link's partial
text By. partialLinkText( < linktext > ) text
By CSS driver.findElement Locates an element using the CSS
By. cssSelector( < cssselector > ) selector
By XPath driver.findElementBy. xpath( < xpath > ) Locates an element using XPath
query
Locators Usage
Now let us understand the practical usage of each of the locator methods with the help of
http://www.calculator.net
By ID
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 IDcdensity.
driver.findElement(By.id("cdensity")).sendKeys("10");
By Name
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 IDcdensity.
driver.findElement(By.name("cdensity")).sendKeys("10");
By Class Name
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"));
By Tag Name
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();
By Link Text
This method helps to locate a link element with matching visible text.
driver.findElements(By.linkText("Volume")).click();
By partial link text
This methods helps locate a link element with partial matching visible text.
driver.findElements(By.partialLinkText("Volume")).click();
By CSS
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"));
By XPath
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");
Loading [MathJax]/jax/output/HTML-CSS/jax.js