使用 aria-label 使用 Python3 和 Selenium 定位并单击元素 [英] Using aria-label to locate and click an element with Python3 and Selenium
问题描述
我想分别点击或更多,展开随时"按钮.我试图通过 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="任何国家/地区"><div class="mn-hd-txt">任何国家/地区</div><span class="mn-dwn-arw"></span><;/div><div class="hdtb-mn-hd" aria-haspopup="true" role="button" tabindex="0" aria-label="任何时间"><div class="mn-hd-txt">任何时间</div><span class="mn-dwn-arw"></span></div><div class="hdtb-mn-hd" aria-haspopup="true" role="button" tabindex="0" aria-label="所有结果"><div class="mn-hd-txt">所有结果</div><span class="mn-dwn-arw"></span></div>
使用 aria-label
属性,您可以尝试以下 xpath
:
driver.find_element_by_xpath("//div[@aria-label='Any time']/div[@class='mn-hd-txt' and text()='Any time']");
<块引用>
或
driver.find_element_by_xpath("//div[@aria-label='任何时间']/div[@class='mn-hd-txt'][text()='任何时间']");
如果使用 aria-label
属性不是强制性要求,您可以使用以下内容:
driver.find_element_by_xpath("//div[@class='hdtb-mn-hd']/div[@class='mn-hd-txt' and text()='Any time']");
<块引用>
或
driver.find_element_by_xpath("//div[@class='hdtb-mn-hd']/div[@class='mn-hd-txt'][text()='任何时候']");
I want to click or more respectively, expand the "Any time" button. I've tried to locate the element by class_name and xpath. The problem is that the class and xpath are the same for all three 'options'. So, I would like to select and click or expand on that element by using the aria-label. I found a couple of suggestions, but it didn't work for me. Most importantly, I try to do that in python 3. I also tried:
driver.find_element_by_xpath(""" //div*[@aria-label='Any Time'] """).click()
but it doesn't work.
Could anyone please help me? Many thanks in advance!
<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>
Using the aria-label
property you can try the following xpath
:
driver.find_element_by_xpath("//div[@aria-label='Any time']/div[@class='mn-hd-txt' and text()='Any time']");
OR
driver.find_element_by_xpath("//div[@aria-label='Any time']/div[@class='mn-hd-txt'][text()='Any time']");
If using aria-label
property is not a mandatory requirement you can use the following:
driver.find_element_by_xpath("//div[@class='hdtb-mn-hd']/div[@class='mn-hd-txt' and text()='Any time']");
OR
driver.find_element_by_xpath("//div[@class='hdtb-mn-hd']/div[@class='mn-hd-txt'][text()='Any time']");
这篇关于使用 aria-label 使用 Python3 和 Selenium 定位并单击元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!