如何解决如何使用 python selenium Web 驱动程序打印 div 类值的元素
如何从下面的代码段中打印 class="valueValue-3kA0oJs5" 的值,尝试了下面的事情但没有运气。
<div class="valuesAdditionalWrapper-3BfAIuML">
<div class="valueItem-3JDGGSt_">
<div class="valueValue-3kA0oJs5" style="color: rgb(128,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"]')
请帮我解决这个问题。
解决方法
print(driver.find_element_by_xpath('//div[@class="valueValue-3kA0oJs5"]').text)
只需在找到的元素上使用 .text。
,如果您想从元素中获取文本,请在 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')