Steerpike
Steerpike

Reputation: 1853

Selenium Webdriver, check if multiple elements are enabled and interact with them if true

I have a page with 16 identical WebElements (buttons) which may or may not be present on the page. The buttons have the same functionality - removing a piece of data when clicked. I need to:-

  1. Check if the elements are visible to the user
  2. If 1. is true, click on them to remove

My code currently:-

public void removeExistingPeriods() {

    List<WebElement> removeButtons = driver.findElements(By.cssSelector(".activeLink.removeBtn.btn.btn-warning"));
    if (removeButtons.size() == 0) {
        fail("No Remove buttons were found on the page!:");
    } else {
        for(WebElement option: removeButtons){
            option.click();
        }
    }

}

This fails with:- org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with

How can I:-

  1. check how many buttons from the 16 total are enabled?
  2. click on those that are enabled in turn until no more enabled buttons are present?

Upvotes: 1

Views: 7257

Answers (3)

Helping Hands
Helping Hands

Reputation: 5396

This will work :

public void removeExistingPeriods() {

List<WebElement> removeButtons = driver.findElements(By.cssSelector(".activeLink.removeBtn.btn.btn-warning"));
if (removeButtons.size()> 0 && removeButtons.isDisplayed()) {

        for(WebElement option: removeButtons){
        option.click();
      } 
      else 
      {

          fail("No Remove buttons were found on the page!:");


         }
      }

     }

I have just reverse if result and button checking condition.

Upvotes: 0

murali selenium
murali selenium

Reputation: 3927

I expecting that you are looking for enabled list of elements. Below code helps you

int totalEnabledElements = 0;

    try{

        List<WebElement> removeButtons = driver.findElements(By.cssSelector(".activeLink.removeBtn.btn.btn-warning"));
        if(removeButtons.size()>0){

            for(int i=0; i<removeButtons.size(); i++){

                if(removeButtons.get(i).isEnabled()==true){

                    removeButtons.get(i).click();
                    totalEnabledElements++;

                }else{
                    System.out.println("element not visible or hidden :" +removeButtons.get(i).getText());
                }

            }

            System.out.println("total enabled elements: " +totalEnabledElements);

        }else{

            System.out.println("No Remove buttons were found on the page!");
        }

    }catch(Exception e){
        System.out.println(e.getStackTrace());
    }

in above code is used isEnabled() as you saying as enabled elements, if you are intend to check displayed elements then you can use isDisplayed.

Let me know, if i am wrong.

Thank You, Murali

Upvotes: -1

Cathal
Cathal

Reputation: 1338

try this.Using the enhanced for loop you loop over each WebElement in removedButtons. If the button isDisplayed then click it.

List<WebElement> removeButtons = driver.findElements(By.cssSelector(".activeLink.removeBtn.btn.btn-warning"));
for(WebElement button : removeButtons) {
  if(button.isDisplayed()) {
     button.click();
  }
}

Upvotes: 2

Related Questions