XPath get an element based on sibling value (single, previous, following)

XPath (XML Path Language) is a language used to navigate XML documents and identify elements within an XML structure. In the context of Selenium, XPath is often used to locate elements on a web page to interact with them using WebDriver.

In Selenium, XPath can be used as a locator strategy to find elements based on their attributes, text content, hierarchical relationships, or other characteristics. With XPath, you can construct expressions that traverse the HTML structure and select specific elements or groups of elements.

To get the sibling element value based on text using XPath in WebDriver, you can use the following-sibling axis along with the text() function. Here’s an example:

Getting the following sibling (single)

# Assuming you have initialized and navigated to a webpage using WebDriver

# Find the element with the specific text
element_with_text = driver.find_element_by_xpath("//span[text()='Text']")

# Get the sibling element value
sibling_value = element_with_text.find_element_by_xpath("following-sibling::span").text

# Print the sibling value
print(sibling_value)

we first locate the element with the specific text using driver.find_element_by_xpath(“//span[text()=’Text’]”). You can modify the XPath expression according to the specific HTML structure and element type you are targeting.

Then, we use the following-sibling::span axis to select the sibling element of the located element. Modify the element type (span in this case) to match the actual type of the sibling element you want to retrieve.

Finally, we retrieve the text value of the sibling element using the text() function and the .text property in Selenium WebDriver. The sibling value is then printed or can be used as needed in your script.

Getting the following sibling (all)

To get all sibling element values based on text using XPath in WebDriver, you can use the following-sibling axis along with the text() function. Here’s an example:

# Find the element with the specific text
element_with_text = driver.find_element_by_xpath("//span[text()='Text']")

# Get all sibling elements
sibling_elements = element_with_text.find_elements_by_xpath("following-sibling::*")

# Get the values of all sibling elements
sibling_values = [element.text for element in sibling_elements]

# Print the sibling values
for value in sibling_values:
    print(value)

Here, we first locate the element with the specific text using driver.find_element_by_xpath(“//span[text()=’Text’]”). Modify the XPath expression according to the specific HTML structure and element type you are targeting.

Then, we use the following-sibling::* axis to select all sibling elements of the located element. This will select all elements regardless of their specific type.

Next, we iterate over the sibling elements and retrieve the text value of each element using the .text property in Selenium WebDriver. The sibling values are stored in the sibling_values list.

Finally, we print the sibling values using a loop or use them as needed in your script.

Getting the previous sibling (all)

To get the previous sibling element value based on text using XPath in WebDriver, you can use the preceding-sibling axis along with the text() function. Here’s an example:

# Find the element with the specific text
element_with_text = driver.find_element_by_xpath("//span[text()='Text']")

# Get the previous sibling element value
sibling_value = element_with_text.find_element_by_xpath("preceding-sibling::span").text

# Print the sibling value
print(sibling_value)

we first locate the element with the specific text using driver.find_element_by_xpath(“//span[text()=’Text’]”). You can modify the XPath expression according to the specific HTML structure and element type you are targeting.

Then, we use the preceding-sibling::span axis to select the previous sibling element of the located element. Modify the element type (span in this case) to match the actual type of the sibling element you want to retrieve.

Finally, we retrieve the text value of the sibling element using the text() function and the .text property in Selenium WebDriver. The sibling value is then printed or can be used as needed in your script.

XPath is a powerful and flexible way to locate elements in WebDriver, especially when other locator strategies like ID or class name are not available or suitable. However, it’s important to construct XPath expressions carefully, considering the HTML structure and making them robust to changes.