简体   繁体   English

使用xpath单击aria-label元素

[英]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存在几个错误,这些错误使其无效:

  1. First of all, you have to state contains before each condition: 首先,必须在每个条件之前声明contains

     "//button[contains(@attribute1, \\"content1\\") and contains(@attribute2, \\"content2\\")]" 
  2. 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! 祝好运!

如何定位元素<div aria-label></div><div id="text_translate"><p>我在 selenium java 中非常初级。 我正在尝试在亚马逊中找到该元素。</p><p> 当我尝试运行此代码时,我找不到该元素。 请帮助解决这个问题。</p><p> <a href="https://i.stack.imgur.com/eMWTB.png" rel="nofollow noreferrer">在此处输入图像描述</a></p><p>这是我的代码</p><pre> WebElement a_review= driver.findElement(By.xpath("//div[@aria-label='4 Stars &amp; Up']")); ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", a_review); a_review.click();</pre></div> - how to locate the element in <div aria-label>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何定位元素<div aria-label></div><div id="text_translate"><p>我在 selenium java 中非常初级。 我正在尝试在亚马逊中找到该元素。</p><p> 当我尝试运行此代码时,我找不到该元素。 请帮助解决这个问题。</p><p> <a href="https://i.stack.imgur.com/eMWTB.png" rel="nofollow noreferrer">在此处输入图像描述</a></p><p>这是我的代码</p><pre> WebElement a_review= driver.findElement(By.xpath("//div[@aria-label='4 Stars &amp; Up']")); ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", a_review); a_review.click();</pre></div> - how to locate the element in <div aria-label> 如何通过带有 Python Selenium 的 aria-label 找到并单击 JavaScript 按钮? - How to find and click a JavaScript button by aria-label with Python Selenium? NVDA 在点击时读取按钮 aria-label 但不在 hover 上 - NVDA is reading button aria-label on click but not on hover 的JavaScript。 通过aria-label获取所有元素 - Javascript. get all element by aria-label Javascript。 通过 aria-label 和 class 获取所有元素 - Javascript. get all element by aria-label and class 在按钮上添加 aria-label - Add aria-label on button (jQuery) 将元素内的标题作为 aria-label 应用到 div 内且不包含 aria label 的链接 - (jQuery) Apply the title that is inside an element as an aria-label to the link that is inside the div and does not contain an aria label 使用键盘导航时无法读取aria-label属性 - aria-label attribute not being read when navigating using keyboard 使用 jquery 从 html 获取 aria-label - Get aria-label from html using jquery 我需要使用链接文本作为 aria 标签将 aria-label 添加到网站上的所有链接 - I need to add aria-label to all links on the website using the link text as aria label
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM