简体   繁体   English

如何使用 xpath 从 DOM 中的节点获取特定文本

[英]How to fetch particular text from the node in DOM using xpath

Consider an DOM structure of angular based application as Follows,考虑基于 angular 的应用程序的 DOM 结构如下,

I need to fetch particular text "disabled" from the above Node.我需要从上面的节点中获取“已禁用”的特定文本。 Kindly put your suggestions how I could manage to get that particular text?请提出您的建议,我如何设法获得该特定文本?

I tried for an solution as,我尝试了一个解决方案,

HTML code eg ("<div id="button" class="login-link s-btn_primary" disabled>") HTML 代码例如("<div id="button" class="login-link s-btn_primary" disabled>")

 String value = driver.findElement(By.xpath("//div[@id='button']").getattribute("innerHTML").trim();

You can first take an element like你可以先取一个元素,比如

WebElement element = driver.findElement(By.xpath("//div[@id='button']"));

and then check if it has the attribute (if it has, assign the required value to a string):然后检查它是否具有属性(如果有,将所需的值分配给字符串):

String fetched = 
        element
                .findElements(By.xpath(".[@disabled]"))
                .size() == 1 ? "disabled" : "";

The latter code means that you check if the attribute has disabled attribute.后一个代码意味着您检查属性是否具有disabled属性。 If yes, you assign disabled value to your string, if not, you assign empty value.如果是,则为字符串分配disabled值,如果不是,则分配空值。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM