【问题标题】:Click a aria-label element using xpath使用 xpath 单击 aria-label 元素
【发布时间】:2018-02-02 17:10:40
【问题描述】:

我无法使用 Xpath 和 Selenium 单击按钮,在这种情况下,aria-label 具有唯一的日期,可以完美区分日历中的其他按钮。

这是显示第 5 天和第 当天的价格。


<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>

#假设我要点击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()

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

【问题讨论】:

  • 也许可以尝试使用 chrome 开发工具,在源面板中选择按钮,然后在控制台面板中尝试 monitorEvents($0) 以了解事件发生了什么

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


【解决方案1】:

您的 xpath 有几个错误,导致它无效:

  1. 首先,你必须在每个条件前声明contains

    "//button[contains(@attribute1, \"content1\") and contains(@attribute2, \"content2\")]"
    
  2. xpath 中的' 不会被转义。请尝试以下操作:

    "//button[contains(@class,\"CalendarDay__button\") and contains(@aria-label,\"Choose monday, february 5 2018 as your check-in date. It's available.\")]"
    

希望对您有所帮助。祝你好运!

【讨论】:

  • 感谢您抽出宝贵时间回答我的问题,不幸的是,我仍然收到相同的消息:dtd0_button.Click() AttributeError: 'WebElement' object has no attribute 'Click'
  • @Petit 我已经编辑了我的答案。似乎问题出在contains 的单引号上。请重新检查!
  • 除了GalAbra的变化,你可能还需要使用方法“click()”,小写“c”
  • 太好了,终于成功了!!谢谢你们,我真的被这个困住了。
猜你喜欢
  • 2021-03-21
  • 2018-03-22
  • 1970-01-01
  • 2017-10-07
  • 1970-01-01
  • 2021-06-19
  • 2020-06-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多