使用咏叹调标签定位并单击一个元素与Python3和硒
问题描述:
我想分别点击或更多,展开“任何时间”按钮。我试图通过class_name和xpath来定位元素。问题在于,类和xpath对于所有三个“选项”都是相同的。因此,我想通过使用aria标签选择并单击或展开该元素。我发现了一些建议,但它对我并不适用。最重要的是,我试图在python 3中做到这一点。我也试过:使用咏叹调标签定位并单击一个元素与Python3和硒
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_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']");
如果使用aria-label
属性不是强制性要求,你可以使用以下命令:
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']");
非常感谢您的回复,@DebanjanB!所有的建议都可以工作,但是,我仍然无法点击它,或者分别扩展它。 我收到以下错误消息: '消息:未知错误:元素
对于'元素不可点击的点(x,y)'看到这个[**'Discussion/QA' **](https://stackoverflow.com/questions/44912203/selenium-web-driver-java-element -is-未点击-在点-36-72-其他-EL/44916498#44916498) – DebanjanB