【问题标题】:How to print element of div class value using python selenium web driver如何使用 python selenium web 驱动程序打印 div 类值的元素
【发布时间】:2021-04-11 08:58:49
【问题描述】:

如何从 sn-p 下面打印 class="valueValue-3kA0oJs5" 的值,尝试了下面的东西,但没有运气。

<div class="valuesAdditionalWrapper-3BfAIuML">
    <div class="valueItem-3JDGGSt_">
        <div class="valueValue-3kA0oJs5" style="color: rgb(128, 0, 128);">46.2625</div>
    </div>
</div>

使用类名:

driver.find_element_by_class_name("valueValue-3kA0oJs5")

使用 xpath:

driver.find_element_by_xpath('//*[@class="valueValue-3kA0oJs5"]')

driver.find_element_by_xpath('//div[@class="valueValue-3kA0oJs5"]')

请帮帮我。

【问题讨论】:

标签: python selenium selenium-webdriver findelement


【解决方案1】:
print(driver.find_element_by_xpath('//div[@class="valueValue-3kA0oJs5"]').text)

只需在找到的元素上使用 .text。

【讨论】:

    【解决方案2】:

    如果您想从元素中获取文本,请在 find_element 方法中使用 text 属性

    value = driver.find_element_by_xpath('//*[contains(@class, "valueValue")]').text
    

    如果要抓取div元素的class属性值

    classname = driver.find_element_by_xpath('//*[contains(@class, "valueValue")]').get_attribute('class')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-03
      • 2016-09-28
      • 2018-07-24
      • 2019-04-30
      相关资源
      最近更新 更多