I’m attempting to extract "479" from this sample HTML:
<div data-testid="testid">
"479"
" Miles Away"
</div>
I’m using the following Selenium code in Python:
xpath = 'html/body/div/text()[1]'
WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH, xpath)))
distance = driver.find_element(By.XPATH, xpath)
print(distance)
Which returns the following error:
'The result of the xpath expression "html/body/div/text()[1]" is: [object Text]. It should be an element.'
I’ve attempted to remove text()[1]
from the end of my xpath, theoretically printing off all data contained the in the HTML div, but it will instead print a blank line when I do so.
Note: I’m an amateur and self-taught (via mostly Google, YouTube, and this site), so some of my wordage may not be correct. I apologize in advanced.
4
Answers
The problem is that you can’t treat text like that, the text() function returns everything as a string including a line break. I think there is no split function that can help you with that, I advise you to get the text in a python variable and do a split(‘n’) to the text.
You should take the entire element (without text()) using only
then from returned element get text, which will be: "479" " Miles Away" .
Then using split method from python you can take that number(split by n, space, or ").
Selenium doesn’t support the following xpath
xpath = 'html/body/div/text()[1]'
To identify the element uniquely, Your xpath should be like
To get the text of the element you have to use
element.text
Given the html:
Both the texts
479
andMiles Away
are with in 2 different text nodes.Selenium doesn’t supports
text()
as it returns a text node, where as Selenium expects back a WebElement. Hence you see the error:Solution
To extract the text 479 you can use either of the following locator strategies:
Using xpath through
execute_script()
and textContent:Using xpath through
splitlines()
andget_attribute()
: