Locators in Selenium WebDriver :
- Locators provide a way to access the HTML elements from a web page to perform operations on it.
- W can use locators to perform particular actions on the text boxes, links, check boxes and other web elements to perform operations on it.
- The locator can be termed as an address that identifies a web element uniquely or may be some times dynamic within the web page.
Locate WebElement from WebPage in WebDriver we locate using below method :
1 | driver.findElement(By.locator("")); |
- Using above mentioned method to locate elements but the locator() part will be replaced with the locator names for e.g: id, name, xPath etc…
Methods to Locate WebElement from web page using Different Locators in Selenium WebDriver :
By ID:
- ID’s are unique for each WebElement so it is common way to locate elements using ID Locator.
- ID locators are the fastest and safest locators out of all locators.
1 2 3 | // Locate Element using id driver.findElement(By.id("Element_ID")); |
By XPath:
- XPath is designed to allow navigation of XML documents, in which the specific elements for specific processing are designed to select individual elements, attributes or some other part of an XML document.
- XPath produces reliable locator, but this is slow in performance (especially in earlier versions of IE) than the CSS selector.
1 2 3 | // Locate Element using xpath driver.findElement(By.xpath("Element_xpath")); |
By Name:
- These are not unique on one page.
- If there are many elements with the same name locator then the first element on the page is selected.
1 2 3 | // Locate Element using name driver.findElement(By.name("Element_NAME")); |
By ClassName:
- The classname locator gives the weblament that matches the values specified in the attribute name “class”.
1 2 3 | // Locate Element using className driver.findElement(By.className("Element_CLASSNAME")); |
By TagName:
- A locator called a tag is used to find elements matching the specified TagName.
- It is very helpful when we want to extract the content within a tag.
1 2 3 | // Locate Element using tagName driver.findElement(By.tagName("Element_TAGNAME")); |
By LinkText:
- If there are many elements with the same link text then the first will be selected.
- This LinkText locator works only on links (hyperlinks), so it is called LinkText locator
1 2 3 | // Locate Element using linkText driver.findElement(By.linkText("Element_LINKTEXT")); |
By PartialLinkText:
- In some situations, we may have to get a link to a part of the text in the LinkText element, this includes.
- In such situations, we use partial links to detect elements
1 2 3 | // Locate Element using partialLinkText driver.findElement(By.partialLinkText("Element_Partial_LINKTEXT")); |
By cssSelector:
- Most automation testers believe that using the CSS selector, the script executes faster than the XPath locator.
- This locator is always the best way to detect elements on the page.
1 2 3 | // Locate Element using cssSelector driver.findElement(By.cssSelector("td[class='someclass'][value='unique text']")); |
Example of Find Locators in Selenium:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class Locators { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Locate Element using id driver.findElement(By.id("Element_ID")); // Locate Element using xpath driver.findElement(By.xpath("Element_XPATHEXPRESSION")); // Locate Element using name driver.findElement(By.name("Element_NAME")); // Locate Element using className driver.findElement(By.className("Element_CLASSNAME")); // Locate Element using tagName driver.findElement(By.tagName("Element_TAGNAME")); // Locate Element using linkText driver.findElement(By.linkText("Element_LINKTEXT")); // Locate Element using partialLinkText driver.findElement(By.partialLinkText("ElePartial_LINK")); // Locate Element using cssSelector driver.findElement(By.cssSelector("td[class='someclass'][value='unique text']")); } } |
Absolute XPath:
- Absolute XPath starts with root node of WebPage.
- It is also called as complete or full XPath.
- Absolute XPath starts from <html> tag.
- it starts from single slash(/).
Example:
1 | html/head/body/form/table/tbody/tr/th |
Relative Xpath:
- A relative xpath is one where the path starts from the node of your choice in WebPage.
- it doesn’t need to start from the root node.
- It starts with Double forward slash(//).
Example:
1 | //*[@id=’login’]/ul/li[3]/a |
Example of Absolute and Relative Xpath:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.Select; import org.openqa.selenium.By; public class Xpath_Selection { public static void main(String[] args) { System.setProperty("webdriver.firefox.marionette","C:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Absolute xpath driver.findElement(By.xpath("html/body/table/tbody/tr[1]/td")); // Relative xpath driver.findElement(By.xpath(".//*[@id='menuicon']")); } } |