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

前端 未结 9 1990
感情败类
感情败类 2020-12-02 12:55

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 str

相关标签:
9条回答
  • 2020-12-02 13:36

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

    0 讨论(0)
  • 2020-12-02 13:41

    if you don't care about isDisplayed or scrolling position, you can also write

    String text = ((JavaScriptExecutor)driver).executeScript("return $(arguments[0]).text();", element);
    

    or without jquery

    String text = ((JavaScriptExecutor)driver).executeScript("return arguments[0].innerText;", element);
    
    0 讨论(0)
  • 2020-12-02 13:42
    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

    0 讨论(0)
  • 2020-12-02 13:43

    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();
    
    0 讨论(0)
  • 2020-12-02 13:48

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

    0 讨论(0)
  • 2020-12-02 13:49

    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!

    0 讨论(0)
提交回复
热议问题