dan-klasson dan-klasson - 2 months ago 22
Python Question

Selenium text not working

I have this span tag:

<span class="pricefield" data-usd="11000">$11,000</span>


And I want to get the text for it (
$11,000
). Naturally I should be using
text
like so:

# empty
print self.selenium.find_element_by_css_selector(".pricefield").text


But it prints nothing.

This doesn't work either:

# None
print self.selenium.find_element_by_css_selector(".pricefield").get_attribute("text")
# None
print self.selenium.find_element_by_css_selector(".pricefield").get_attribute("value")


However, I can get the elements attributes just fine:

# 11000
print self.selenium.find_element_by_css_selector(".pricefield").get_attribute("data-usd")


Why doesn't this work? Did something change in Selenium? I am using 2.52.0.

Guy Guy
Answer

get_attribute("textContent") or get_attribute("innerText") are the attributes you are looking for.