Selenium Locators

Selenium is a popular tool for automation testing, and it provides different locators to find elements on a web page. Here are some commonly used locators in Selenium:


ID: This locator is used to find elements with a specific ID attribute on a web page. For example:
	driver.findElement(By.id("elementId"));


Name: This locator is used to find elements with a specific name attribute on a web page. For example:
	driver.findElement(By.name("elementName"));

Class Name: This locator is used to find elements with a specific class name on a web page. For example:
	driver.findElement(By.className("className"));


Tag Name: This locator is used to find elements with a specific HTML tag name on a web page. For example:
	driver.findElement(By.tagName("tagName"));

Link Text: This locator is used to find elements with a specific link text on a web page. For example:
	driver.findElement(By.linkText("linkText"));

Partial Link Text: This locator is used to find elements with a specific partial link text on a web page. For example:

	driver.findElement(By.partialLinkText("partialLinkText"));

CSS Selector: This locator is used to find elements with a specific CSS selector on a web page. For example:

	driver.findElement(By.cssSelector("cssSelector"));


XPath: This locator is used to find elements with a specific XPath expression on a web page. For example:

driver.findElement(By.xpath("xpathExpression"));

Each locator has its advantages and disadvantages, and the choice of locator depends on the specific scenario and the structure of the web page being tested.


XPath is a commonly used locator in Selenium, which is used to find elements on a web page based on the XML path expression. XPath can be used to locate elements based on their tag name, attribute value, text content, and other properties.

Here are some examples of using XPath in Selenium:

Finding elements by tag name:
	driver.findElement(By.xpath("//tagName"));

Finding elements by attribute value:
	driver.findElement(By.xpath("//*[@attributeName='value']"));

Finding elements by partial attribute value:

	driver.findElement(By.xpath("//*[contains(@attributeName,'partialValue')]"));

Finding elements by text content:

	driver.findElement(By.xpath("//*[text()='textContent']"));

Finding elements by partial text content:

	driver.findElement(By.xpath("//*[contains(text(),'partialTextContent')]"));

Finding child elements:

	driver.findElement(By.xpath("//parentTagName/childTagName"));

Finding sibling elements:

	driver.findElement(By.xpath("//tagName/following-sibling::siblingTagName"));

XPath can be a powerful tool for locating elements, but it's important to use it carefully, since complex expressions can be slow to execute and may be prone to breaking if the page structure changes. It's a good practice to test XPath expressions in a browser console before using them in Selenium scripts.



Followers