Reputation: 1853
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:-
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:-
Upvotes: 1
Views: 7257
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
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
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