2 minute read

Absolute XPath(1/2)

It's a direct path from the root element to the desired element. It starts from the root node and ends with the desired node, providing a complete path. However, it's brittle and can break with small changes in the web page's structure. Here's a Selenium XPath example for how you might use an absolute XPath in your Selenium code. Suppose you have the following HTML structure:

Advertisement

Absolute XPath(2/2)

To find the <p> tag using an absolute XPath, you would start at the root <html> tag and provide the full path to the <p> tag:

WebElement paragraph = driver.findElement(By.xpath("/html/body/div/p"));

In this case, the XPath "/html/body/div/p" represents the absolute path from the root <html> tag to the desired <p> tag.

In several scenarios, however, absolute XPath is not recommended unless necessary because it's brittle, and any change in the HTML structure may cause your test to fail.

Relative XPath(1/2)

It starts from any node and ends with the node you want to select. It's more flexible and preferred in most cases, as it's not affected by changes in other parts of the HTML structure. A relative XPath allows you to locate elements starting from any location within the HTML document, not just the root. The relative XPath expression usually starts with //.

```python from selenium import webdriver

# Create a new instance of the Firefox driver driver = webdriver.Firefox()

# Navigate to a website driver.get("https://example.com")

# Find an element using relative XPath element = driver.find_element_by_xpath("//div[@id='myDiv']/p[2 ]")

# Perform actions on the element element.click()

Here's an example of using relative XPath in Selenium with Python:

# Close the browser driver.quit(

Relative XPath(2/2)

In the example mentioned in the previous slide , we first import the necessary modules from the Selenium library. Then, we create a new instance of the Firefox driver and navigate to a website (in this case, "https://example.com").

Next, we find an element using a relative XPath expression. The XPath used in this example selects the second `<p>` element inside a `<div>` element with the id "myDiv". You can modify the XPath expression to suit your specific needs.

After finding the element, you can perform various actions on it, such as clicking it, entering text, or retrieving its attributes.

Finally, we close the browser using the `quit()` method to clean up and release the resources used by the driver.

Using relative XPaths is generally recommended over absolute XPaths because they are more resilient to page structure changes.

Types of XPath locators

XPath locator by ID

This locator allows you to identify an element by its id attribute.

XPath locator by name

This locator identifies elements by their name attribute.

XPath locator by text

This locator identifies elements based on their inner text.

XPath locator using starts-with

This locator identifies elements whose attribute values start with a particular string.

XPath locator by class name

This locator can identify elements based on their class attribute.

XPath locator by tag name

This locator can identify elements by their HTML tag name.

XPath locator using contains

This locator can identify elements based on a substring of one of their attribute values.

Path locator using ends-with

This locator can identify elements whose attribute values end with a particular string.

This article is from: