最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Click a aria-label element using xpath - Stack Overflow

programmeradmin0浏览0评论

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>

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.

Share Improve this question asked Feb 2, 2018 at 17:10 PetitPetit 231 gold badge1 silver badge6 bronze badges 1
  • Maybe try to use chrome dev tools, selecting the button in the source panel, then try monitorEvents($0) in console panel to figure out what's going on with events – Gilles Quénot Commented Feb 2, 2018 at 17:14
Add a ment  | 

1 Answer 1

Reset to default 5

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!

发布评论

评论列表(0)

  1. 暂无评论