【问题标题】:{WebElement}.getAttribute("value") returns 0{WebElement}.getAttribute("value") 返回 0
【发布时间】:2020-06-25 18:02:37
【问题描述】:

我正在与网页中的元素交互:

<li class="MuiButtonBase-root MuiListItem-root MuiMenuItem-root jss524 MuiMenuItem-gutters jss525 MuiListItem-gutters MuiListItem-divider MuiListItem-button" tabindex="-1" role="menuitem" aria-disabled="false" data-quid="SelectListItem-2" value=“{URL}”><div class="jss519"><div class="jss520"><div>{TEXT}</div></div><div class="jss522"></div></div><span class="MuiTouchRipple-root"></span></li>

在我的代码中,我有这个 sn-p:

            System.out.println(x.getAttribute("role"));
            System.out.println(x.getAttribute("aria-disabled"));
            System.out.println(x.getAttribute("data-quid"));
            System.out.println(x.getAttribute("class"));
            System.out.println(x.getText());
            System.out.println(x.getAttribute("value"));

但是,最后一行 System.out.println(x.getAttribute("value")); 打印出 0 而不是实际值。

我正在使用硒 3.141.59

【问题讨论】:

  • 你的x 是什么?你是如何识别你的元素的。
  • 可能是您通过 findElement 函数获取的元素不正确
  • @SameerArora WebElement x = this.webDriver.findElement(By.xpath("{XPATH VALUE}"));它打印出其他属性的实际值
  • @RR_IL 我可以打印出其他属性的实际值
  • 你是如何分配 value 属性的?您是否以某种方式动态分配它?

标签: java selenium selenium-webdriver automation qa


【解决方案1】:

您可以尝试使用 JavascriptExecutor。

protected String getInputValue(WebElement element) {
String js = "return arguments[0].querySelector('input').value";
JavascriptExecutor js = (JavascriptExecutor) driver;
return js.executeScript(js, element).toString();
}

参考: Selenium Webdriver get input value which already has value attribute (Java)

【讨论】:

  • 我能够调整 String js 的值以使我的代码正常工作!我仍然不明白为什么我们必须强制使用 JSExecutor,但它确实使我的测试工作!
猜你喜欢
  • 2015-03-23
  • 2021-03-09
  • 1970-01-01
  • 2021-10-05
  • 1970-01-01
  • 2010-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多