Home > Blockchain >  Selenium - How to check if a "Load More" button is clickable/disabled?
Selenium - How to check if a "Load More" button is clickable/disabled?

Time:05-28

I gotta scroll down a list of articles from a Blog until I find a specific article. For that there is a "Load More" button which gives you more and more articles until there's no more articles to load, so the button disappears, it gets disabled (at least thats what I thought).

My idea was to have a "while" loop until the button gets disabled, meanwhile it would check if the requested article showed up:

public void scrollIntoArticle(String title){
        while(loadMoreBtn.isEnabled()){
            for(WebElement article : articlesList){
                if(article.getText().equals(title)){
                    scrollInto(article);
                    hoverAndClick(article);
                    break;
                } else{
                    scrollInto(lastArticle());
                    hoverAndClick(loadMoreBtn);
                }
            }
        }
    } 

However, it keeps getting an error: "waiting for visibility of element". Trying with "isDisplayed" I get the same issue.

So apparently the button never gets "disabled". The only thing that happens to the element is to get a new attribute on HTML: style="display: none;". For Selenium, "isEnabled" is always true, so the loop keeps going, "waiting for visibility of element".

I am stuck, cannot figure out what condition I can use to check if all the articles got loaded up without checking the "Load More" button. Any suggestions? Appreciate the help!

CodePudding user response:

You can wait for element's attribute changed, just use ExpectedCondition class for example:

    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15));
wait.until(ExpectedConditions.attributeToBe(buttonWebElement, "style", "display: none;")

or wait for element displayed with the attribute, for example:

wait.visibilityOf(driver.findElement(By.xpath("//button[@style='display: none;']")))

CodePudding user response:

In the end, I used try/catch for the Load More button. So when it could not be more "clickable" it would throw an exception.

"isDisplayed", "isEnabled", ExpectedConditions.attributeToBe as mentioned by others did not work.

Here's the code:

public void goToArticle(String title){
        int count = 0;
        while(true){
            for(WebElement article:articlesLink){
                if(article.getText().equals(title)){
                    scrollIntoCenter(article);
                    clickArticle(article);
                    break;
                } else {
                    scrollIntoCenter(lastArticle());
                }
            }
            try {
                hoverAndClick(loadMoreBtn);
            } catch (Exception e) {
                System.out.println("No more load button");
                count  ;
                if (count > 1){
                    System.out.println("No article found");
                    break;
                }
            }
        }

    }
  • Related