Monday, 22 September 2014

Selenium : Locate element in web browser using Element Locator Tool

There are various way to local element on web browser. We can use located element in selenium script to do automate things.
There are various locator types :
  • C# Locator
  • Java Locator
  • XPath Locator
  • Ruby Locator

We can Locate element by various way like by ID, Type, Name, CSS Class, etc..

Here are examples of locator strings which are useful in C# code to get value or perform any action on that element :

  • driver.FindElement(By.XPath("//input[@id='gbqfq']"));
  • driver.FindElement(By.XPath("//input[@name='q']"));
  • driver.FindElement(By.XPath("//input[@role='combobox']"));
  • driver.FindElement(By.XPath("//input[contains(@id,'gbqfq')]"));

Now to locate an element first you need to download and install FireFox Element Locator Add-on.

Selenium Tool : Download and Install FireFox Element Locator Add-on.

After installing FireFox Element Locator Add-on you need to do just right click on any element in FireFox Web browser, after clicking on element you can see the context menu, In context menu you can see various locator type like C# Locator, Java Locator, XPath Locator, Ruby Locator, etc.

On clicking on that particular menu you can see list of valid locator string with green color right tick mark, by clicking on that particular locator string it will copy that string in your clipboard and you can paste at any place where you want to use this element locator string.

[Image : Element Locator Menu] :

Selenium : Locate element in web browser using Element Locator Tool
(To view original size image , click on image)

Full Video : 

1 comment: