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

时间:2017-10-10 14:54:44

标签: python selenium xpath

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

<Path Fill="Aqua" Stroke="Gray" StrokeThickness="2">
    <Path.Data>
        <CombinedGeometry>
            <CombinedGeometry.Geometry1>
                <RectangleGeometry Rect="0,0,400,200" RadiusX="20" RadiusY="20"/>
            </CombinedGeometry.Geometry1>
            <CombinedGeometry.Geometry2>
                <PathGeometry Figures="M180,200 L200,250 100,200"/>
            </CombinedGeometry.Geometry2>
        </CombinedGeometry>
    </Path.Data>
</Path>

但它不起作用。

有人可以帮帮我吗?非常感谢提前!

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

2 个答案:

答案 0 :(得分:3)

使用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']");

答案 1 :(得分:1)

所以,最近几天我一直在为此苦苦挣扎,事实证明,这令人头疼。 aria标签基本上是唯一可靠的属性,而xpath解决方案不适用于我。

一时兴起,我尝试使用:

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

其中XXXX是我要搜索的aria标签。像魅力一样工作。

所有这些,请尝试使用css选择器。 行之有效。