Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python and Selenium - get text excluding child node's text

Using Python 3.

Supposing:

<whatever>
  text
  <subchild>
    other
  </subchild>
</whatever>

If I do:

elem = driver.find_element_by_xpath("//whatever")

elem.text contains "text other"

If I do:

elem = driver.find_element_by_xpath("//whatever/text()[normalize-space()]")

elem is not Webelement.

How my I proceed to grab only "text" (and not "other")?

Id est: grab only text in direct node, not the child nodes.

UPDATE:

Original HTML is:

<div class="border-ashes the-code text-center">
VIVEGRPN
  <span class="cursor"></span>
  <button class="btn btn-ashes zclip" data-clipboard-target=".the-code" data-coupon-code="VklWRUdSUE4=">
  <span class="r">Hen, la.</span>
</div>
like image 779
Álvaro N. Franz Avatar asked Jul 21 '17 13:07

Álvaro N. Franz


2 Answers

Bear in mind that the replacement approach mentioned by @Guy doesn't work for many structures.

For instance, having this structure:

<div>
    Hello World
    <b>e</b>
</div>

The parent text would be Hello World e, the child text would be e, and the replacement would result in Hllo World instead of Hello World.

A safe solution

To get the own text of an element in a safe manner, you have to iterate over the children of the node, and concat the text nodes. Since you can't do that in pure Selenium, you have to execute JS code.

OWN_TEXT_SCRIPT = "if(arguments[0].hasChildNodes()){var r='';var C=arguments[0].childNodes;for(var n=0;n<C.length;n++){if(C[n].nodeType==Node.TEXT_NODE){r+=' '+C[n].nodeValue}}return r.trim()}else{return arguments[0].innerText}"
parent_text = driver.execute_script(OWN_TEXT_SCRIPT, elem)

The script is a minified version of this simple function:

if (arguments[0].hasChildNodes()) {
    var res = '';
    var children = arguments[0].childNodes;
    for (var n = 0; n < children.length; n++) {
        if (children[n].nodeType == Node.TEXT_NODE) {
            res += ' ' + children[n].nodeValue;
        }
    }
    return res.trim()
}
else {
    return arguments[0].innerText
}
like image 173
jcrs Avatar answered Nov 15 '22 19:11

jcrs


You can remove the child node text from the all text

all_text = driver.find_element_by_xpath("//whatever").text
child_text = driver.find_element_by_xpath("//subchild").text

parent_text = all_text.replace(child_text, '')
like image 29
Guy Avatar answered Nov 15 '22 21:11

Guy