我在从以下页面获取文本元素(“1,028 Sales”)时遇到问题:https://www.etsy.com/shop/susansimonini?ref=l2-shopheader-name 。
我可以使用这个毫无问题地获取元素本身:
driver.find_element_by_class_name('shop-sales')
但我无法将其转换为文本。以下结果产生空行:
sales=driver.find_element_by_class_name('shop-sales').text
print sales
我做错了什么?预先感谢您的帮助。
乔什
最佳答案
.text
返回一个空字符串,因为目标元素已隐藏。
因此,您可以定位保存销售数量的可见部分:
text = driver.find_element_by_css_selector(".shop-info .shop-location + span").text
或者您可以直接获取 innerHTML
或 textContent
属性:
text = driver.find_element_by_css_selector(".shop-sales").get_attribute("textContent")
text = driver.find_element_by_css_selector(".shop-sales").get_attribute("innerHTML")
关于Python Selenium : get element by class and convert to text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36702664/