Get Tag Using Text Selenium Beautifulsoup Python

I know there's a way using xpath and javascript

    element = browser.find_element_by_xpath("//*[contains(text(),'text')]")

but this method doesn't detect elements/tags, that are defined just as tags, eg:

      <span class="text-primary">UK</span>
      +44 (0) 1865 987 667<br>
      Piccadilly Gardens, 49 Piccadilly, Manchester, M1 2AP </p>

In this case, if the text is +44 (0) 1865 987,it does not get the element.

  1. This issue is repetitive in many examples, that incorporates the text this way. What could be the reason?
  2. Is there a way to get the tag, searching using text, in beautifulsoup?


My expectation is that you need to use the following functions combination:

  1. normalize-space() - to look for matches in children/ignore leading/trailing whitespaces, etc.
  2. contains() - for the partial match

Putting everything together:

element = driver.find_element_by_xpath("//*[contains(normalize-space(),'+44 (0) 1865 987 667')]")


enter image description here

More information: XPath Operators & Functions