Python Selenium : get element by class and convert to text

标签 python selenium text

我在从以下页面获取文本元素(“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

或者您可以直接获取 innerHTMLtextContent 属性:

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/

相关文章:

Python Pandas group by 然后根据条件进行过滤

java - 如何在 selenium webdriver 中选择周范围值

sql - 需要根据单词列表在 Teradata SQL 中搜索文本字段并返回该单词

python - 使用 Python 或 Applescript 从网页中提取文本

python - 使用 glDraw 数组对立方体进行纹理处理时出现问题

Python 子进程 - 在创建的新命令提示符中运行第二个命令

c++ - C++ 中的 Python 代码

selenium - 如何修复 docker 容器中的 "org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: crashed"

python - 抓取的网站数据未写入 CSV

javascript - ContentEditable - 获取当前字体颜色/大小