[英]Click a aria-label element using xpath
I am unable to click a button using Xpath and Selenium, in this case the aria-label has a unique date which make it perfect to distinguish among some other buttons from a calendar. 我无法使用Xpath和Selenium单击按钮,在这种情况下,aria标签具有唯一的日期,因此可以完美地区分日历中的其他按钮。
This is the HTML code for the button that display the day 5 and the price for that day.
这是按钮的HTML代码,显示第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>
#Let say I want to click february 5 2018, I tried #假设我想点击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()
what is wrong with this approach and I receive the following message 'WebElement' object has no attribute 'Click' if I can Click for any date in the calendar on the web page. 这种方法有什么问题,如果我可以单击网页日历中的任何日期,则收到以下消息“ WebElement”对象没有属性“单击”。
There are a couple of errors with your xpath, which make it invalid: 您的xpath存在几个错误,这些错误使其无效:
First of all, you have to state contains
before each condition: 首先,必须在每个条件之前声明
contains
:
"//button[contains(@attribute1, \\"content1\\") and contains(@attribute2, \\"content2\\")]"
The '
inside your xpath isn't escaped. xpath内部的
'
不会转义。 Try the following: 请尝试以下操作:
"//button[contains(@class,\\"CalendarDay__button\\") and contains(@aria-label,\\"Choose monday, february 5 2018 as your check-in date. It's available.\\")]"
Hope it helps. 希望能帮助到你。 Good luck!
祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.