如何从该 WebElement 获取 aria-label 属性的值?

问题描述 投票:0回答:1

使用 Java Selenium,我有一个对代表以下 HTML 节点的

WebElement
对象的引用:

<td role="gridcell" class="mat-calendar-body-cell ng-star-inserted" style="width: 25%; padding-top: 7.14286%; padding-bottom: 7.14286%;" tabindex="-1" data-mat-row="0" data-mat-col="0" aria-label="2001" aria-selected="false">...</td>

我需要 aria-label 属性的

2001
值,但我似乎无法做到。以下方法调用都返回
null
,我不知道为什么:

  • element.getAttribute("aria-label");
  • element.getAttribute("ariaLabel");
  • element.getDomAttribute("aria-label");
  • element.getDomAttribute("ariaLabel");
  • element.getDomProperty("aria-label");
  • element.getDomProperty("ariaLabel");

同时尝试

aria-label
ariaLabel
的原因是它在属性列表中列为
ariaLabel

我打印了该节点的所有其他属性的值,以验证这不是计时问题,奇怪的是,这就是输出:

role: gridcell
class: mat-calendar-body-cell-container ng-star-inserted
style: width: 25%; padding-top: 7.14286%; padding-bottom: 7.14286%;
tabindex: null
data-mat-row: 0
data-mat-col: 0
aria-label: null
aria-selected: null

如何获取

aria-label
的值?

硒版本:4.15.0

Chrome驱动程序版本:122.0.6261.57

解决了

我犯了一个极其愚蠢的错误。显然,我自动化的网站版本已更新,将

aria-label
属性从
td
节点移动到子
button
节点;我只是还没有费心刷新页面..

java html selenium-webdriver dom selenium-chromedriver
1个回答
0
投票

试试这个:

element.ariaLabel;

参考:
https://developer.mozilla.org/en-US/docs/Web/API/Element/ariaLabel

© www.soinside.com 2019 - 2024. All rights reserved.