Josh
Josh

Reputation: 105

Python Selenium: get element by class and convert to text

I am having trouble getting the text element ("1,028 Sales") from the following page: https://www.etsy.com/shop/susansimonini?ref=l2-shopheader-name.

I can get the element itself with no issue using this:

driver.find_element_by_class_name('shop-sales') 

But I cannot convert it to text. The following results in a blank line:

sales=driver.find_element_by_class_name('shop-sales').text
print sales

What am I doing wrong? Thanks in advance for your help.

josh

Upvotes: 2

Views: 2492

Answers (1)

Florent B.
Florent B.

Reputation: 42528

The .text returns an empty string because the targeted element is hidden. So you can either target the visible one holding the sales count :

text = driver.find_element_by_css_selector(".shop-info .shop-location + span").text

Or you could directly get the innerHTML or textContent property :

text = driver.find_element_by_css_selector(".shop-sales").get_attribute("textContent")
text = driver.find_element_by_css_selector(".shop-sales").get_attribute("innerHTML")

Upvotes: 2

Related Questions