What is element locator in selenium

Element locator in selenium

Element locators in selenium plays very crucial role. Selenium find element using element locator. Selenium webdriver is automation testing tool which interacts directly with UI elements same as real user interacts with them.

element locator in selenium

Selenium do not have any built in feature to locate element itself. To perform any action on UI element, We have to provide element locator in selenium test script. Selenium will find that element from page based on provided element locator and then it will perform given action on it.

Let's consider practical example. You have a login page with 3 different elements User ID textbox, Password textbox and Login button. Now you want to write a selenium test script for user login. Your selenium test script will look like below.

Locate User ID field
WebElement UIDtxt = driver.findElement(By.Element locator of user id field);

Locate Password field
WebElement Passtxt = driver.findElement(By.Element locator of Password field);

Locate Login button field
WebElement Loginbtn = driver.findElement(By.Element locator of Login button);

Type my user id in User ID field.

Type my password in Password field.
Passtxt.sendKeys("my password");

Click on Login button.

In above given example you can see then we have provided element locator of each field to locate element. After identification of element, Selenium will perform given action on that element.

There are many different element locator strategies in selenium. List of selenium element locator strategy is as below.
  • ID - Locate element by ID of element.
  • Name - Locate element by Name of element.
  • ClassName - Locate element by class name of element.
  • linkText - Locate element by link text of element.
  • partialLinkText - Locate element by partial link text of element.
  • cssSelectors - Locate element by CSS selector of element.
  • XPath - Locate element by XPath of element.
  • tagName - Locate element by tag name of element.
We will learn about all above mentioned element locators one by one.

No comments:

Post a Comment