python - Selenium Python 如何从 <div> 获取文本(html 源)

标签 python html selenium tags

我正在尝试获取文本 $27.5内部标签 <div> ,我通过 id 定位元素,该元素称为“价格”。

html片段如下:

<div id="PPP,BOSSST,NYCPAS,2015-04-26T01:00:00-04:00,2015-04-26T05:20:00-04:00,_price" class="price inlineBlock strong mediumText">$27.50</div>

这是我试过的

price.text
price.get_attribute('value')

以上两个都不行。

更新: 感谢所有试图提供帮助的人。 我将您的答案结合在一起并得到了解决方案:)

    price = driver.find_element_by_xpath("//div[@class='price inlineBlock strong mediumText']")
    price_content = price.get_attribute('innerHTML')
    print price_content.strip()

最佳答案

难道不能用正则表达式或者Beautiful Soup在HTML中查找元素的内容吗:

re.search(r'<div.*?>(*.?)</div>', price.get_attribute('innerHTML')).group(1)

关于python - Selenium Python 如何从 <div> 获取文本(html 源),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29807856/

相关文章:

python - GRPC 流式选择(python)

javascript - 在 Javascript 中选择两个按钮时如何使条件发生?

java - 发生断言错误后 Chrome 不会退出

java - SVG 无法找到我在自动化脚本中验证所需的图形元素的 xpath

javascript - 神经网络知识数据如何存储?

Python 日志记录 : create log if not exists or open and continue logging if it does

python - 使用 zipmodule 重命名 zip 文件夹中的文件

javascript - 使用 JS 将值附加到新数组

html - 如何在转换后的元素中保留元素的固定位置?

python - 按下按钮然后下载 csv 文件