1

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:

    <p>
      <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?
2
  • Can there be more than one occurrence within the html? Commented Sep 14, 2019 at 12:51
  • @QHarr yes. There can be. I found a solution by searching for all the tags using selenium, and then matching with the call string. I included a and p for the tags. However, it can get generic than that. Commented Sep 14, 2019 at 13:18

2 Answers 2

1

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')]")

Demo:

enter image description here

More information: XPath Operators & Functions

Sign up to request clarification or add additional context in comments.

Comments

0

In Selenium you can try with Sub string matching.

text="+44 (0) 1865 987 667"
print(WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,"//*[contains(.,'" + text + "')]"))).text)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.