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

前端 未结 9 1991
感情败类
感情败类 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:50

    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.

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

    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

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

    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.

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