简体   繁体   中英

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.

This is the HTML code for the button that display the day 5 and the price for that day.


 <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

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.

There are a couple of errors with your xpath, which make it invalid:

  1. First of all, you have to state contains before each condition:

     "//button[contains(@attribute1, \\"content1\\") and contains(@attribute2, \\"content2\\")]" 
  2. The ' inside your xpath isn't escaped. 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM