无法在 XPath 中使用 text() 获取文本 [英] Cannot get text using text() in XPath

查看:47
本文介绍了无法在 XPath 中使用 text() 获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是部分html代码:

Here is part of html code:

<td class="audit-context-break-word text-left" nowrap="nowrap">
<b class="ng-binding">PONumber</b></td>
<td class="audit-context-break-word text-left ng-binding">20202022  02_001  </td>

我只需要获取里面的文本(预期值:20202022 02_001)我尝试了以下变体:

I need to get only text inside (expected value: 20202022 02_001) I tried following variants:

By.xpath("/descendant::*[.='PONumber']/../descendant::*[@class=\"audit-context-break-word text-left ng-binding\"]/text()")
By.xpath("/descendant::*[.='PONumber']/../following::text()[1]")

问题是在 FirePath 中找到了选定的文本,但未在测试中.由于超时异常而失败,因为找不到元素

The issue is that in FirePath selected text is found, but not in test. It is failed due to timeout exception, because element is not found

失败的原因是什么?

推荐答案

您可以使用 following-sibling 来编写 Xpath.

You can use following-sibling to write the Xpath.

WebElement element=driver.findElement(By.xpath("//td[b[contains(text(),'PONumber')]]/following-sibling::td"));
        String text = element.getText();
        System.out.println(text);

  1. 我正在确定网页上合适的网络元素并将其保存在网络元素中.

  1. I'm identifying appropriate web element on the webpage and saving it in web element.

使用 getText() 方法从该 Web Element 获取文本并将其保存为字符串

Using getText() method to obtain text from that web Element and saving it in string

打印该字符串text

这篇关于无法在 XPath 中使用 text() 获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆