gpt4 book ai didi

python - 使用 aria-label 定位并单击带有 Python3 和 Selenium 的元素

转载 作者:太空宇宙 更新时间:2023-11-03 15:51:17 25 4
gpt4 key购买 nike

我想分别点击一个或多个,展开“随时”按钮。我试图通过 class_name 和 xpath 定位元素。问题是所有三个“选项”的类和 xpath 都是相同的。因此,我想使用 aria-label 选择并单击或展开该元素。我找到了一些建议,但对我没有用。最重要的是,我尝试在 python 3 中这样做。我也尝试过:

driver.find_element_by_xpath(""" //div*[@aria-label='Any Time'] """).click()

但它不起作用。

谁能帮帮我?非常感谢!

<div class="hdtb-mn-hd" aria-haspopup="true" role="button" tabindex="0" aria-label="Any country"><div class="mn-hd-txt">Any country</div><span class="mn-dwn-arw"></span></div>
<div class="hdtb-mn-hd" aria-haspopup="true" role="button" tabindex="0" aria-label="Any time"><div class="mn-hd-txt">Any time</div><span class="mn-dwn-arw"></span></div>
<div class="hdtb-mn-hd" aria-haspopup="true" role="button" tabindex="0" aria-label="All results"><div class="mn-hd-txt">All results</div><span class="mn-dwn-arw"></span></div>

最佳答案

所以,在过去的几天里,我一直在努力解决这个问题,事实证明这是一个令人头疼的问题。 aria-label 基本上是唯一可靠的属性,xpath 解决方案对我不起作用。

一时兴起,我尝试使用:

driver.find_elements_by_css_selector("[aria-label=XXXX]")

其中 XXXX 是我用单引号搜索的咏叹调标签(例如“[aria-label='More']”)。工作起来很有魅力。

综上所述,请尝试使用 css 选择器。它很管用。

关于python - 使用 aria-label 定位并单击带有 Python3 和 Selenium 的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46669850/

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