Ad
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:
<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.
- This issue is repetitive in many examples, that incorporates the text this way. What could be the reason?
- Is there a way to get the tag, searching using text, in beautifulsoup?
Ad
Answer
My expectation is that you need to use the following functions combination:
- normalize-space() - to look for matches in children/ignore leading/trailing whitespaces, etc.
- contains() - for the partial match
Putting everything together:
element = driver.find_element_by_xpath("//*[contains(normalize-space(),'+44 (0) 1865 987 667')]")
Demo:
More information: XPath Operators & Functions
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad