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

Viewed 7594

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>
4 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
}

You can firstly extract the outerHTML from the element, then build the soup with BeautifulSoup, and remove any element you want.

Small example:

el = driver.find_element_by_css_selector('whatever')
outerHTML = el.get_attribute('outerHTML')
soup = BeautifulSoup(outerHTML)
inner_elem = soup.select('subchild')[0].extract()
text_inner_elem = inner_elem.text
text_outer_elem = soup.text
Related