getText() method of selenium chrome driver sometimes returns an empty string

Viewed 72267

I have a curious case where the selenium chrome driver getText() method (java) returns an empty string for some elements, even though it returns a non-empty string for other elements with the same xpath. Here is a bit of the page.

<div __gwt_cell="cell-gwt-uid-223" style="outline-style:none;">
<div>Text_1</div>
<div>Text_2</div>
<div>Text_3</div>
<div>Text_4</div>
<div>Text_5</div>
<div>Text_6</div>
</div>

for each of the inner tags, I can get valid return values for getTagName(), getLocation(), isEnabled(), and isDisplayed(). However, getText() returns an empty string for some of the divs.

Further, I notice that if I use the mac chrome driver, it is consistently the ‘Text_5’ for which getText() returns an empty string. If I use the windows chrome driver, it is , it is consistently the ‘Text_2’ for which getText() returns an empty string. If I use the firefox driver, getText() returns the expected text from all the divs.

Has anyone else had this difficulty?

In my code, I use something like this…

ArrayList<WebElement> list = (ArrayList<WebElement>) driver.findElements(By.xpath(“my xPath here”));
for (WebElement e: list) System.out.println(e.getText());

As suggested below, here is the actual xPath I am using. The page snippet above deals with the last two divs.

//*[@class='gwt-DialogBox']//tr[contains(@class,'data-grid-table-row')]//td[contains(@class,'lms-assignment-selection-wizard-cell')]/div/div
10 Answers

element.getAttribute("innerText") worked for me, when getText() was returning empty.

I encountered a similar issue recently.

I had to check that the menu tab "LIFE EVENTS" was present in the scroll box. The problem is that there are many menu tabs and you are required to scroll down to see the rest of the menu tabs. So my initial solution worked fine with the visible menu tabs but not the ones that were out of sight.

I used the xpath below to point selenium to the parent element of the entire scroll box.

@FindBy(xpath = "//div[contains(@class, 'menu-tree')]")
protected WebElement menuTree;

I then created a list of WebElements that I could increment through. The solution worked if the menu tab was visible, and returned a true. But if the menu tab was out of sight, it returned false

public boolean menuTabPresent(String theMenuTab) {
    List<WebElement> menuTabs = new ArrayList<WebElement>();
    menuTabs = menuTree.findElements(By.xpath("//i/following-sibling::span"));

    for(WebElement e: menuTabs) {
        System.out.println(e.getText());
        if(e.getText().contains(theMenuTab)) {
            return true;
        }
    }
    return false;
}

I found 2 solutions to the problem which both work equally well.

    for(WebElement e: menuTabs) {
        scrollElementIntoView(e); //Solution 1
        System.out.println(e.getAttribute("textContent")); //Solution 2
        if(e.getAttribute("textContent").contains(theMenuTab)) {
            return true;
        }
    }
    return false;

Solution 1 calls the method below. It results in the scroll box to physically move down while selenium is running.

protected void scrollElementIntoView(WebElement element) {
    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true)", element);
}

Solution 2 gets the text content (even for the menu tabs not currently visible) of the attribute that you are pointing to. Thus doing the job properly that .getText() was not able to do in this situation.

Mine is python, but the core logic is similar:

  • webElement.text
  • webElement.get_attribute("innerText")
  • webElement.get_attribute("textContent")

Full code:

def getText(curElement):
    """
    Get Selenium element text

    Args:
        curElement (WebElement): selenium web element
    Returns:
        str
    Raises:
    """
    # # for debug
    # elementHtml = curElement.get_attribute("innerHTML")
    # print("elementHtml=%s" % elementHtml)

    elementText = curElement.text # sometime NOT work

    if not elementText:
        elementText = curElement.get_attribute("innerText")

    if not elementText:
        elementText = curElement.get_attribute("textContent")

    # print("elementText=%s" % elementText)
    return elementText

Calll it:

curTitle = getText(h2AElement)

hope is useful for you.

Worked for me:

add as a predicate of xpath the length of string greater than 0:

String text = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//span[string-length(text()) > 0]"))).getText();
Related