Selenium Locators

Very basic question what is the locator in selenium, Locators allows us to find elements on a page that can be used in our test.  Actually we need to uniquely identify one element from HTML page where we can do operation like click, select, sendKeys etc.
Selenium webdriver provide us various mechanism to uniquely identify one element from HTML.

1.  Locate elements by ID
2.  Locate elements by Name
3.  Locate elements by Link
4.  Locate elements by Xpath
5.  Locate elements by Css
6.  Locate elements by DOM
7.  Locate elements by PartialLink

Before starting we should begain by making sure that we have all the relevant application installed. While there are not fullproof , they will give us clue how to construct the locator for our test page.


Firefox browser:

1. Firebug: This is a Firefox addon and it allows developer to find elements on the page.

Firebug is available in firefox addon section


firebug install


2. Firepath :

Firepath is available in Firefox addon section.

IE browser:

1. IE Developer tool: This is in build feature for IE ,we can launch by pressing F12.

Chrome:

1. Google chrome developer tool: Like IE this is also in build tool for chrome. By  pressing F12 we can use this tool. We can generate Xpath ,css path using this tool.


find xpath in chrome



copy xpath in chrome
Xapth or css path generation









Now start with locate element by ID

On web application element should have an ID for all there controls on the page. ID should be the unique for that page.

How to find element by ID in selenium 


selenium locators
In the  image you will find the ID of the google search text box.  id="lst-ib".
ID is unique for this page.

Now we will write down java selenium code to uniquely identify the web element (In selenium all web component is a web element) and we will operate on this web element.

Here we are assuming we using Firefox browser
WebDriver driver = new FirefoxDriver();
WebElement we = driver.findElement(By.id("st-ib"));
we.sendKeys("selenium:);

In my next post I will cover below items

1.  Locate elements by Name
2.  Locate elements by Link
3.  Locate elements by Xpath

Share this

Related Posts

Previous
Next Post »