0 votes
1 view
in DevOps and Agile by (19.8k points)

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;">








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.


1 Answer

0 votes
by (63.4k points)

The syntax shown below worked for me when getText() method was returning empty:


You can use it like this:

for (int count=0;count<=sizeofdd;count++) { String GetInnerHTML=getddvalue.get(count).getAttribute("innerHTML"); }

where getddvalue is the WebElement,

sizeofdd is the size of getddvalue

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 online training!

Welcome to Intellipaat Community. Get your technical queries answered by top developers !