How do I find the element with text "Jessica" based on "3000" displaying in the web page using xpath?

I'm getting ...not a valid xpath error.

  <label>Total Wealth ($):</label>

I tried this

RichestPersonName = browser.find_element_by_xpath("//p[text()='3000'] .. /span").text

but it shows

selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //p[text()='3000'] .. /span because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//p[text()='3000'] .. /span' is not a valid XPath expression.

4 Answers 4


i tried this and it works. Managed to get 'Jessica'

RichestPersonName = browser.find_element_by_xpath("//p[text()='3000']/preceding-sibling::span").text


You should use the preceding-sibling Xpath option:

  • this got me '3000'. i'm trying to get 'Jessica' based on '3000'.
    – AskNAnswer
    Jul 22, 2021 at 1:41
  • Sorry, changed that to preceding-sibling. Please retry.
    – Mate Mrše
    Jul 22, 2021 at 7:58

By selecting the correct approach of creating locators can make any automation framework more efficient. Xpath is one of the available options to locate any element in complex html web pages. With the help of XPath, tester can find the exact element by navigating through different hierarchies of web page. Like by using, any of these functions : Contains, Sibling and Parent etc.

In special scenarios where element needs to be located by referring to another element, any professional testing company prefer dynamic techniques of selecting element on a webpage using HTML DOM structure rather than making it hard coded.

Three possible ways to locate desired element in your case are given below :

  1. **/..** : "//p[text()='3000']/../span"

  2. **Preceding-Sibling** : "//p[text()='3000']/preceding-sibling::span"

  3. **Parent** : "//p[text()='3000']/parent::div/span"

These approaches will find the required node span with text 'Jessica' by navigating through other elements.


To find the element with text "Jessica" based on "3000" displaying in the web page using XPath, you can use the following XPath expression:

//element[contains(text(), 'Jessica') and contains(text(), '3000')]

This XPath expression searches for an element that contains both the text "Jessica" and "3000". The contains() function is used to check if the element's text contains the specified substring.

You can use this XPath expression with various programming languages and libraries that support XPath, such as Python's lxml or selenium libraries. Here's an example using Python and the lxml library:

from lxml import etree

# Assuming you have the HTML source stored in a variable called 'html'
tree = etree.HTML(html)

# Finding the element with text "Jessica" and "3000" using XPath
element = tree.xpath("//element[contains(text(), 'Jessica') and contains(text(), '3000')]")

# Accessing the text of the element
if element:
    text = element[0].text
    print("Element not found.")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.