gpt4 book ai didi

python - 如何根据使用 Selenium 的 html 从使用 xpath 找到的元素中检索属性 aria-label 的值

转载 作者:太空宇宙 更新时间:2023-11-04 09:40:13 25 4
gpt4 key购买 nike

我有以下 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com