如何根据使用 Selenium 的 html 从使用 xpath 找到的元素中检索属性 aria-label 的值 [英] How to retrieve the value of the attribute aria-label from element found using xpath as per the html using Selenium
问题描述
我有以下 HTML 跨度:
I have the following HTML span:
<button class="coreSpriteHeartOpen oF4XW dCJp8">
<span class="glyphsSpriteHeart__filled__24__red_5 u-__7" aria-label="Unlike"></span>
</button>
我还有一个 webElement
表示包含我使用 xpath
找到的这个跨度的按钮.如何从元素中检索 aria-label
值(不同)?
I also have a webElement
representing the button containing this span that I have found using xpath
. How can I retrieve the aria-label
value (Unlike) from the element?
我尝试这样做:
btn = drive.find_element(By.xpath, "xpath")
btn.get_attribute("aria-label")
但它什么都不返回.如何检索元素的文本值带有来自元素对象的aria-label"属性?
but it returns nothing. How to retrieve the text value of an element with 'aria-label' attribute from the element object?
推荐答案
aria-label
是 span 元素的属性,而不是按钮.你可以这样得到:
aria-label
is attribute of span element, not button.
You can get it like this:
btn = drive.find_element(By.xpath, "xpath")
aria_label = btn.find_element_by_css_selector('span').get_attribute("aria-label")
或者,如果您的目标是找到包含属性 aria-label="Unlike"
的 span 按钮:
Or if your goal is to find button with span contains attribute aria-label="Unlike"
:
btn = drive.find_element(By.XPATH, '//button[./span[@aria-label="Unlike"]]')
#you can add class to xpath also if you need
btn = drive.find_element(By.XPATH, '//button[./span[@aria-label="Unlike"] and contains(@class,"coreSpriteHeartOpen)]')
这篇关于如何根据使用 Selenium 的 html 从使用 xpath 找到的元素中检索属性 aria-label 的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!