I’m wondering if it’s possible to just return "Glenvale" from the below HTML with Selenium:
I’ve tried using the Xpath, but that doesn’t seem to work.
suburb = driver.find_element(By.XPATH, '//*[@id="__next"]/div/div[2]/div/div[4]/div/div/div[8]/div/div/p/text()[6]').text
below is the website:
https://www.domain.com.au/2-76-shelby-street-glenvale-qld-4350-2014406153
2
Answers
According to the HTML you’ve shared, the content that you want to filter is actually the text content of the p tag. So, you wont be able to get the output by relying on locators only. You can however, use regex to your advantage. Simply put, get the textContent of the
<p>
tag and then extract out the detail you want to zero in on. Sharing an example of the same approach.This script will capture the textContent of the p tag which is
37 other 3 bedroom unit in Glenvale have recently been sold. There are currently 7 properties for sale in Glenvale.
and using the regex will capture and return 1 location which in this case is "Glenvale".NOTE: The regex can be modified as per your need. If you need to capture the stats of properties and bedroom unit sold. Just update the regex to contain the required capture groups.
Algorithm:
<p>
nodeGlenvale.
Code:
Result:
Be aware: This solution will capture the last word in the sentence of
<p>
node. If the last word is notGlenvale.
then it will capture whatever the last word is.