Locators in Selenium Webdriver

Element Locators:

I we say that element locators strategy is the back-bone of web-driver then it won't be wrong. As Selenium Web-Driver provides us various methods in order to locate web-element. Below are the details one-by-one:

1) Locating element by Id*:

If your web-element has a static and unique id then this is the best way to locate. Syntax is:

driver.findElement(By.id("id of your webelement")).click();

*It won't work in case, where id is generating dynamically and not static.

2) Locating element by Name:

Locating Element By Name and Locating Element by ID are nearly same. In Locating Element by ID method, webdriver will look for the specified ID attribute and in Locating Element By Name method, webdriver will look for the specified Name attribute. Syntax is:

driver.findelement(By.name("name of your webelement")).click();

3) Locate element by ClassName:

What if element doesn't have id and name? In this case, locate an element by ClassName is a good way. Syntax is:

driver.findElement(By.className("name of the class")).click();

4) Locate element by TagName:

Let say, web-element do not have any ID/ Class-Name/Name then how will you locate that element in selenium Web-Driver ? There could be many alternatives of selenium Web-Driver element locators and one of them is Locating Element By Tag Name. Locating Element By Tag Name is not too much popular because, we will have other alternatives of element locators in most of the cases. But yes if there is not any alternative then you can use element's DOM Tag Name to locate that element in webdriver.

driver.findElement(By.tagName("tag name")).click();

5) Locate element by Link Text:

We have very popular and reliable way in order to find out the hyperlinks on a web-page. Let say, if you have a "About Us" link on a page then the syntax would be:

driver.findElement(By.linkText("About Us")).click();

6) Locate element by Partial Link Text:

This locator method is very much similar to Link Text but the basic difference is that this method locates the element by taking the partial text from the link. Let’s say, we have "Click Me" link and the syntax would be:

driver.findElement(By.partialLinkText("Click")).click();

7) Locate element by cssSelector:

cssSelector is one of the most popular method among the different locator methods. If you have DOM like this:

<input type = "text" name = "FirstName">

Then syntax would be:

driver.findElement(By.cssSelector("input[name='FirstName']")).click();

You can get the csspath from firbug also.

8) Locate element by XPath:
Last method of locating element in selenium webdriver is By XPath. It is most popular and best way to locate element in WebDriver. If you have DOM  like this:

<input type = "text" name = "FirstName">
 Syntax:

Driver.findElement(By.xpath(“//input[@name=’FirstName’]”)).click();

No comments:

Post a Comment

|Software Testing Glossary Series|CTFL|N|O|P|

Non-Functional Requirement: Non-Functional Requirement is a requirement that describes how the component or system will do what it is s...