selenium - Counting inner text letters of HTML element -
is there way count letters of inner text of html element, without counting letters of inner element's texts?
i tried out ".gettext()" method of "webelements" using selenium library, counts inner texts of inner web elements in (e.g. "<body><div>test</div></body>" results in 4 letters "div" and "body" element, instead of 0 "body" element)
do have use additional html parsing library, , when yes 1 recommend?
i'm using java 7...
based on this answer similar question, cooked solution:
the piece of javascript takes element, iterates on child nodes , if they're text nodes, reads them , returns them concatenated:
var element = arguments[0]; var text = ''; (var = 0; < element.childnodes.length; i++) if (element.childnodes[i].nodetype === node.text_node) { text += element.childnodes[i].textcontent; } return text;
i saved script script.js
file , loaded single string
via fileutils.readfiletostring()
. can use guava's files.tostring()
, too. or embed java code.
final string script = fileutils.readfiletostring(new file("script.js"), "utf-8"); javascriptexecutor js = (javascriptexecutor)driver; ... webelement element = driver.findelement(by.anything("myelement")); string text = (string)js.executescript(script, element);
Comments
Post a Comment