How to exclude text content of child elements of the parent element in Selenium WebDriver is explained in this post.

We can achieve this with help of jQuery.

To understand this topic one should know, how to execute jQuery with Selenum WebDriver, refer previous post run jQuery

To explain this, took the website as an example. In the bottom of this page it has supported languages, those are maintained in a parent div tag with child tags. I want to get the text of the parent tag only, that is “ offered in:”

If we write the locator for the parent div tag and then get the text with WebDriver Api how to buy Phenytoin online getText(),

  • WebElement supLanguages=driver.findElement(By.cssSelector("div#_eEe"));

It will give you all the supported languages along with text in parent, that is “ offered in: हिन्दी  বাংলা  తెలుగు  मराठी  தமிழ்  ગુજરાતી  ಕನ್ನಡ  മലയാളം  ਪੰਜਾਬੀ “

How to Exclude text content OF child element

Following are the two ways


  1. $("div#_eEe").clone().children().remove().end().text();
    • Here clone() method will clone the entire element
    • children() method will get the all children’s of the parent element
    • remove() method will remove the fetched children’s
    • end() method is used for filtering option that will return the cloned parent again.
    • text() method will return the text in the parent.
  2. $("div#_eEe").contents().filter(function(){ return this.nodeType == 3; }).text();
    • Here contents() will get the childrens of the matched elements, including text and comment nodes.
    • Applied filter to get the parent node text content.
    • nodeType property returns the node type in number format
      • return #1 if the node is an element node
      •  return 3 if the node is a text node.

Below is the complete code.

package com.exploreselenium;

import java.lang.Class;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;


public class RunJquery {

    WebDriver driver;
    JavascriptExecutor js;

    @FindBy(id="lst-ib") WebElement googleTxtBox;
    @FindBy(css="div#_eEe") WebElement supportedLanguages;

    public void browserInstantiate() {
        //System.setProperty("", System.getProperty("user.dir") + "\\chromedriver.exe");
        System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "\\geckodriver.exe");
        driver=new FirefoxDriver();
        //driver = new ChromeDriver();
        PageFactory.initElements(driver, this);

    public void getTextFromParent() throws Exception{
        WebElement supportedLanguages=driver.findElement(By.cssSelector("div#_eEe"));
        System.out.println(js.executeScript("return $(\"div#_eEe\").clone().children().remove().end().text();"));

    public void enablejQuery() throws Exception{
        String readyState = js.executeScript("return document.readyState").toString();
        System.out.println("Ready State: " + readyState);
        System.out.println("Is Jquery loaded......" + isjQueryLoaded());
        if(!isjQueryDefined()) {
            URL jqueryUrl = Resources.getResource("jquery-3.1.1.min.js");
            String jqueryText = Resources.toString(jqueryUrl, Charsets.UTF_8);
        System.out.println("Is Jquery loaded......" + isjQueryLoaded());

    public Boolean isjQueryDefined() throws Exception {
        return (Boolean) js.executeScript("return !!window.jQuery;");

    public void verifyElementVisible(WebElement element){
        new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOf(element));


Here is the output. It has logged text that is fetched with WebDriver getText() API and jQuery as well.

  • First line fetched with getText()
  • Last line fetched with jQuery
  • offered in: हिन्दी বাংলা తెలుగు मराठी தமிழ் ગુજરાતી ಕನ್ನಡ മലയാളം ਪੰਜਾਬੀ
    Ready State: complete
    Is Jquery loaded......false
    Is Jquery loaded......true offered in:

Here we learned how to fetch text content of a parent element by exclude it’s children element content text with jQuery in Selenium WebDriver.


Run jQuery in Selenium WebDriver