getText() method of selenium chrome driver sometim

2019-01-13 10:14发布

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

7条回答
看我几分像从前
2楼-- · 2019-01-13 10:32

Update: The textContent attribute is a better option and supported across the majority of browsers. The differences are explained in detail at this blog post: innerText vs. textContent

As an alternative, the innerText attribute will return the text content of an element which exists in the DOM.

element.getAttribute("innerText")

The isDisplayed() method can sometimes trip over when the element is not really hidden but outside the viewport; getText() returns an empty string for such an element.

You can also bring the element into the viewport by scrolling to it using javascript, as follows:

((JavaScriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", element);

and then getText() should return the correct value.

Details on the isDisplayed() method can be found in this SO question:

How does Selenium WebDriver's isDisplayed() method work

查看更多
趁早两清
3楼-- · 2019-01-13 10:36

Related to getText() I have also an issue and I resolved so:

WebElement errMsg;
errMsg = driver.findElement(By.xpath("//div[@id='mbr-login-error']"));
WebElement parent = driver.findElement(By.xpath("//form[@id='mbr-login-form']"));
List<WebElement> children = parent.findElements(By.tagName("div")); 
System.out.println("Size is: "+children.size());
//((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", children);
for(int i = 0;i<children.size();i++)
{
    System.out.println(i + " " + children.get(i).getText());
}
int indexErr = children.indexOf(errMsg);
System.out.println("index " + indexErr);
Assert.assertEquals(expected, children.get(indexErr).getText());

None of the above solutions worked for me.

查看更多
我只想做你的唯一
4楼-- · 2019-01-13 10:41

This is not a solution, so I don't know if it belongs in an answer, but it's too long for a comment and includes links, so I'm putting it an answer.

I have had this issue as well. After doing some digging, it seems that the problem arises when trying to get the text of an element that is not visible on the screen.(As @Faiz comments above.)This can happen if the element is not scrolled to, or if you scroll down and the element is near the top of the document and no longer visible after the scroll. I see you have a FindElements() call that gets a list of elements. At least some are probably not visible; you can check this by trying boolean b = webElement.isDisplayed(); on each element in the list and checking the result. (See here for a very long discussion of this issue that's a year old and still no resolution.)

Apparently, this is a deliberate design decision (see here ); gettext on invisible elements is supposed to return empty. Why they are so firm about this, I don't know. Various workarounds have been suggested, including clicking on the element before getting its text or scrolling to it. (See above link for example code for the latter.) I can't vouch for these because I haven't tried them, but they're just trying to bring the element into visiblity so the text will be available. Not sure how practical that is for your application; it wasn't for mine. For some reason, FirefoxDriver does not have this issue, so that's what I use.

I'm sorry I can't give you a better answer - perhaps if you submit a bug report on the issues page they'll see that many people find it to be a bug rather than a feature and they'll change the functionality.

Good luck! bsg

EDIT

See this question for a possible workaround. You won't be able to use it exactly as given if isDisplayed returns true, but if you know which element is causing the issue, or if the text is not normally blank and you can set an 'if string is empty' condition to catch it when it happens, you can still try it. It doesn't work for everyone, unfortunately.

NEW UPDATE I just tried the answer given below and it worked for me. So thanks, Faiz!

查看更多
在下西门庆
5楼-- · 2019-01-13 10:47
for (int count=0;count<=sizeofdd;count++)
{       
   String GetInnerHTML=getddvalue.get(count).getAttribute("innerHTML");
}

where, 1. getddvalue is the WebElement 2. sizeofdd is the size of getddvalue

查看更多
淡お忘
6楼-- · 2019-01-13 10:50

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

查看更多
疯言疯语
7楼-- · 2019-01-13 10:54

WebElement.getAttribute("value") should help you !!

查看更多
登录 后发表回答