使用xpath单击aria-label元素

时间:2018-02-02 17:10:40

标签: javascript python-3.x selenium xpath web-scraping

我无法使用Xpath和Selenium单击按钮,在这种情况下,aria-label具有唯一的日期,这使得它非常适合区分日历中的其他按钮。

  

这是显示第5天和第5天的按钮的HTML代码   那天的价格。



<button type="button" class="CalendarDay__button" aria-label="Choose sunday, february 4 2018 as your check-in date. It's available." tabindex="0"><div class="calendar-day"><div class="day">4</div><div class="flybondi font-p fare-price"><span>$728*</span></div></div></button>
<button type="button" class="CalendarDay__button" aria-label="Choose monday, february 5 2018 as your check-in date. It's available." tabindex="0"><div class="calendar-day"><div class="day">5</div><div class="flybondi font-p fare-price"><span>$728*</span></div></div></button>
&#13;
&#13;
&#13;

#Let say我想点击2018年2月5日,我试过

dtd0_button = driver.find_element_by_xpath("//button[contains(@class, 'CalendarDay__button') and (@aria-label, 'Choose monday, February 5 2018 as your check-in date. It's available') ]")
dtd0_button.Click()

这种方法有什么问题,我收到以下消息&#39; WebElement&#39;对象没有属性&#39;点击&#39;如果我可以点击网页日历中的任何日期。

1 个答案:

答案 0 :(得分:0)

xpath存在一些错误,导致其无效:

  1. 首先,您必须在每个条件之前说明contains

    "//button[contains(@attribute1, \"content1\") and contains(@attribute1, \"content1\")]"
    
  2. xpath中的'未转义。请尝试以下方法:

    "//button[contains(@class,\"CalendarDay__button\") and contains(@aria-label,\"Choose monday, february 5 2018 as your check-in date. It's available.\")]"
    
  3. 希望它有所帮助。祝你好运!