我有以下 HTML 跨度:
<button class="coreSpriteHeartOpen oF4XW dCJp8">
<span class="glyphsSpriteHeart__filled__24__red_5 u-__7" aria-label="Unlike"></span>
</button>
我还有一个 webElement
表示包含我使用 xpath
找到的这个跨度的按钮。如何从元素中检索 aria-label
值(不同于)?
我尝试过:
btn = drive.find_element(By.xpath, "xpath")
btn.get_attribute("aria-label")
但它什么也没返回。如何检索元素的文本值 使用元素对象的“aria-label”属性?
最佳答案
aria-label
是 span 元素的属性,不是按钮。
你可以这样得到它:
btn = drive.find_element(By.xpath, "xpath")
aria_label = btn.find_element_by_css_selector('span').get_attribute("aria-label")
或者,如果您的目标是找到 span 包含属性 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)]')
关于python - 如何根据使用 Selenium 的 html 从使用 xpath 找到的元素中检索属性 aria-label 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52009771/