【问题标题】:get attributeby is not working获取属性不工作
【发布时间】:2014-10-29 09:12:30
【问题描述】:
        String a=driver.findElement(By.id(locator))).getAttribute("value");

        System.out.println("the value is "+ a);

        Log.info(" Required object is present in the applicaiton"+ object);

        <p locale="" isnumeric="false" basetype="String" class="outputText twControl twOutputText" id="InputText12">**MANTPLAP29102014100955627**</p>

我需要从上面的 HTML 代码中获取 MANTPLAP29102014100955627 值。 请帮忙..

【问题讨论】:

  • 在你的情况下,你只需要 getText() 的元素。只有在需要获取内联属性时才需要使用.getAttribute(),例如getAttribute("basetype")
  • 当我使用 basetype.. 它返回字符串。
  • 这是正确的,因为你的元素的内联属性是basetype="String"。因此,要获取元素的 TEXT,请使用 getText()。它是文本,因为它不是内联属性,并且位于 &lt;p&gt;&lt;/p&gt; 标记之间。

标签: java selenium webdriver


【解决方案1】:

使用

driver.findElement(By.id(locator))).getText();

编辑

getAttribute 和 getText 方法有区别

考虑一个这样的html标签

<input type = "text" id = "id" name = "bird">Peagon</input>

现在使用 driver.findElement(By.id("id"))).getAttribute("name") 将返回值bird(这是属性“名称”的值) 并使用

driver.findElement(By.id(locator))).getText(); 将返回一个值作为Peagon(开始和结束&lt;input&gt; 标记之间的文本)

【讨论】:

  • 完美的 Abhishek..它给出了正确的值。感谢分享答案。
  • 有一种情况,当我得到字符串时,我需要在另一个页面中搜索,应该说输入的文本不存在。请帮我编码
  • 请编辑您的答案以添加对您的代码如何工作以及它如何解决 OP 问题的解释。很多 SO 发帖人都是新手,看不懂你贴的代码。
猜你喜欢
  • 1970-01-01
  • 2018-04-08
  • 1970-01-01
  • 2020-08-25
  • 1970-01-01
  • 2022-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多