Selenium-如何在部分文本上使用getText()? [英] Selenium - how to use getText() on partial text?

查看:210
本文介绍了Selenium-如何在部分文本上使用getText()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下一个Web类上使用getText()方法时,我将获得与之关联的全文:

When I'm using the getText() method on the next web class I'm getting the full text associated with it:

$('.row.session-panel.form-group .session-name [href]')[0]

<a href=​"/​10002267/​agenda/​session/​10020933">​"8:30am - 9:45am "<span>​Welcoming Notes​</span>​</a>​

如果我使用getText(),我将获得全文:上午8:30-上午9:45欢迎致辞" .有没有办法只采用 8:30 am-9:45 am 而无需应用Java的特殊方法(例如substring())?

If I use the getText() I'm getting the full text: "8:30am - 9:45am Welcoming Notes". Is there any way getting only the 8:30am - 9:45am without applying Java's special methods like substring()?

推荐答案

看起来您想要的只是由a元素中的文本节点直接表示的文本. Selenium中目前没有一种方法可以在不获取其子元素的文本的情况下获取元素的文本.您可以执行以下操作直接从DOM中提取文本.

It looks like what you want is just the text which is directly represented by the text nodes in your a element. There is currently no method in Selenium that allows to get the text of an element without also getting the text of its children. You can do the following to extract the text from the DOM directly.

String text = (String) ((JavascriptExecutor) driver).executeScript(
    "var parent = arguments[0]; "+
    "var child = parent.firstChild; "+
    "var ret = ""; "+
    "while(child) { "+
    "    if (child.nodeType === Node.TEXT_NODE) "+
    "        ret += child.textContent; "+
    "    child = child.nextSibling; "+
    "} "+
    "return ret;", a);

(自从我定期使用Java编码以来已经有一段时间了.这是从Python代码转换而来的.表示它的方法可能更好.)

(It's been a while since I've coded on Java on a regular basis. This is converted from Python code. There may be a nicer way to represent it.)

变量a将是您已经使用Seleniums方法之一找到元素的锚.如果您需要更好的东西(例如,删除空格或不需要的其他多余字符),而又在Java方面无法做到这一点,则可以将其添加到上述JavaScript方面的代码中.例如,如果要摆脱开头和结尾的空格,可以使用return ret.trim().

The variable a would be the anchor you've already found using one of Seleniums methods for finding elements. If you need something finer (e.g. remove spaces or any other extraneous character you don't want) and somehow cannot do it on the Java side, you could add it to the code above on the JavaScript side. For instance you could have return ret.trim() if you want to get rid of leading and trailing space.

我已经在Chrome,Firefox和IE 10-11上使用了上述方法,没有出现问题.我看不到最基本的DOM级别未涵盖的任何内容,因此我希望它可以在任何浏览器上正常工作.

I've used the method above on Chrome, Firefox and IE 10-11 without problems. I do not see anything there that is not covered by the most basic levels of DOM so I would expect it to work on any browser.

这篇关于Selenium-如何在部分文本上使用getText()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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