Search This Blog

Sunday, July 30, 2017

Locators in Selenium

In simple words, Locators are similar to tracking the location of your Ola/Uber Cab on road. 

Once a web page loads on your browser there are several elements on the webpage for user to perform action on. There can be Input boxes such as username/password, Submit buttons, Dropdown lists, Texts, Radio buttons etc.

In order to perform action on any element on a webpage, selenium webdriver needs to know the location of the webelement. There are 8 distinct locators that are used to access webelements via Selenium webdriver:

1. Id | example: WebElement We = driver.findElement(By.id("username"));

2. Name | example: WebElement We = driver.findElement(By.name("submit"));

3. Linktext | example: WebElement We = driver.findElement(By.linkText("link Text"));

4. Partial Linktext | example: WebElement We = driver.findElement(By.PartialLinkText("link Text"));

5. Tag Name | example: Select se = new Select(driver.findElement(By.tagName("menu"))); select.selectByVisibleText("Jan");

6. Class Name | example: WebElement We=driver.findElement(By.className(“test”));

7. CSS | example: WebElement We = driver.findElements(By.cssSelector("input[id=password']"));

8. Xpath