Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

<div id="a">This is some

   <div id="b">text</div>

</div>

Getting "This is some" is non-trivial. For instance, this returns "This is some text":

driver.find_element_by_id('a').text

How does one, in a general way, get the text of a specific element without including the text of its children?

(I'm providing an answer below but will leave the question open in case someone can come up with a less hideous solution).

1 Answer

0 votes
by (62.9k points)

Here's a general solution:

def get_text_excluding_children(driver, element): 

         return driver.execute_script(""" return jQuery(arguments[0]).contents().filter(function() { 

                     return this.nodeType == Node.TEXT_NODE; }).text();

                     """, element)

The element passed to the function can be obtained from the find_element...() methods (i.e. it can be a WebElement object).

Or if you don't have jQuery or don't want to use it you can replace the body of the function above with this:

return self.driver.execute_script("""

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;

 """, element)

I'm actually using this code in a test suite.

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s Selenium certification!

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...