【问题标题】:How to find the element part of the anchor tag如何找到锚标签的元素部分
【发布时间】:2019-02-25 21:12:50
【问题描述】:

我对硒完全陌生。对于提出愚蠢或愚蠢的问题,请接受道歉。

我在网站上有以下内容。我感兴趣的是如何使用 selenium + python 获得 data-selectdate 值。获得data-selectdate 值后,我想将其与我感兴趣的日期进行比较。

非常感谢您的帮助。

注意:我没有使用美丽的汤或任何东西。

干杯

<table role="grid" tabindex="0" summary="October 2018">
  <thead>
    <tr>
      <th role="columnheader" id="dayMonday"><abbr title="Monday">Mon</abbr></th>
      <th role="columnheader" id="dayTuesday"><abbr title="Tuesday">Tue</abbr></th>
      <th role="columnheader" id="dayWednesday"><abbr title="Wednesday">Wed</abbr></th>
      <th role="columnheader" id="dayThursday"><abbr title="Thursday">Thur</abbr></th>
      <th role="columnheader" id="dayFriday"><abbr title="Friday">Fri</abbr></th>
      <th role="columnheader" id="daySaturday"><abbr title="Saturday">Sat</abbr></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td role="gridcell" headers="dayMonday">
        <a data-selectdate="2018-10-22T00:00:00+01:00" data-selected="false" id="day22" 
          class="day-appointments-available">22</a>
      </td>
      <td role="gridcell" headers="dayTuesday">
        <a data-selectdate="2018-10-23T00:00:00+01:00" data-selected="false" id="day23" 
          class="day-appointments-available">23</a>
      </td>
      <td role="gridcell" headers="dayWednesday">
        <a data-selectdate="2018-10-24T00:00:00+01:00" data-selected="false" id="day24" 
          class="day-appointments-available">24</a>
      </td>
      <td role="gridcell" headers="dayThursday">
        <a data-selectdate="2018-10-25T00:00:00+01:00" data-selected="false" id="day25" 
          class="day-appointments-available">25</a>
      </td>
      <td role="gridcell" headers="dayFriday">
        <a data-selectdate="2018-10-26T00:00:00+01:00" data-selected="false" id="day26" 
          class="day-appointments-available">26</a>
      </td>
      <td role="gridcell" headers="daySaturday">
        <a data-selectdate="2018-10-27T00:00:00+01:00" data-selected="false" id="day27" 
          class="day-appointments-available">27</a>
      </td>
    </tr>
  </tbody>
</table>

【问题讨论】:

  • 你能分享网站的链接吗,因为它看起来像当前的 html 没有显示相关元素
  • 不幸的是,该 URL 会动态更新日历。所以我认为它不会帮助我们。除此之外,登录凭据后,URL 会出现三四页。
  • 看看 ^ 链接...你会得到 data-selectdate 属性,它会返回你想要的字符串。从那里您可以对现有日期等进行字符串比较。

标签: python-2.7 selenium selenium-webdriver webdriver getattribute


【解决方案1】:

要获取 属性 data-selectdate 的值,您可以使用以下解决方案:

elements = driver.find_elements_by_css_selector("table[summary='October 2018'] tbody td[role='gridcell'][headers^='day']>a")
for element in elements:
    print(element.get_attribute("data-selectdate"))

【讨论】:

  • 谢谢大佬,我试试看。
【解决方案2】:

您可以使用element类的get_attribute api来读取元素的属性值。

css_locator = "table tr:nth-child(1) > td[headers='dayMonday'] > a"
ele = driver.find_element_by_css_selector(css_locator)
selectdate = ele.get_attribute('data-selectdate')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    • 2019-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多