Selenium: Add WebElements to a List, then check visibility of all of them

11,362

Solution 1

Cannot figure out why i can't add these same-type elements to my List -

There is, most probably a typo in your code. It should be element instead of elements :

for (WebElement element : elements){
        webElementList.add(element);
    }

Getting this error on "visibleWebElement" -

driver.findElement() method accepts a By object as an argument, which is a selector. But you are passing visibleWebElements which is a WebElement object and that is why your code is giving an error. For ex, if you want to locate an element by xpath, this is the correct way to use it:

WebElement your_element = driver.findElement(By.xpath("//whatever xpath"));

You can directly use the isDisplayed()method on your WebElement:

public void estaVisible(WebElement visibleWebElements) {

    boolean esvisible =  visibleWebElements.isDisplayed();
    Assert.assertTrue(esvisible);
    return esvisible;
}

Solution 2

When you try to check the list of Web elements you have to check with the visibility of all elements.Try with the below code

WebDriverWait for list of web elements

Store all the elements in a list

List<WebElement> listOfAllWebElements = new ArrayList<WebElement>();
listOfAllWebElements.add(elementOne);
listOfAllWebElements.add(elementTwo);
listOfAllWebElements.add(elementThree);

WebDriverWait listWait = new WebDriverWait(driver,10);
listWait.until(ExpectedConditions.visibilityOfAllElements(listOfAllWebElements));

WebDriverWait for single web element

WebElement element = driver.findElement(By.id("element"));

WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOf(element));
Share:
11,362
SeekanDestroy
Author by

SeekanDestroy

Updated on June 05, 2022

Comments

  • SeekanDestroy
    SeekanDestroy almost 2 years

    Using Selenium and Cucumber, I'm trying to setup a method to store WebElements through args. Then with a second method, I'm trying to check the visibility of all elements on the list.

    I have some elements located this way:

     @FindBy(how = How.XPATH, using = "//* 
     [@id=\"content\"]/section/fieldset/form/div[1]/div[2]/input")
     public WebElement formName;
     @FindBy(how = How.CSS, using = "//* 
     [@id=\"content\"]/section/fieldset/form/div[2]/div[2]/input")
     public WebElement formPassword;
     @FindBy(how = How.ID, using = "remember")
     public WebElement rememberMe;
     @FindBy(how = How.CLASS_NAME, using = "button principal")
     public WebElement loginButton;
    

    Then I wrote this method to add WebElements in a list:

    public void crearListaWebElement(WebElement... elements){
        List<WebElement> webElementList = new ArrayList<>();
        for (WebElement element : elements){
            webElementList.add(elements);
        }
    }
    

    Getting this error on .add(elements) 1º issue:

    add (java.util.Collection) in List cannot be applied to (org.openqa.selenium.WebElement[])

    Cannot figure out why i can't add these same-type elements to my List

    Then, here is the second method to check visibility:

    public void estaVisible(WebDriver(tried WebElement as well) visibleWebElements) {
    
        boolean esvisible =  driver.findElement(visibleWebElements).isDisplayed();
        Assert.assertTrue(esvisible);
        return esvisible;
    }
    

    Getting this error on "visibleWebElement":

    findElement (org.openqa.selenium.By) in WebDriver cannot be applied to (org.openqa.selenium.WebDriver)

    I understand the conflict between the 2 different types of object, but, aren't the objects found by WebDriver stored as WebElements?

    Could someone put some light overhere please? Thanks in advance.  

  • SeekanDestroy
    SeekanDestroy almost 6 years
    Thanks for your reply! Yes, i undertsand the function of WebDriverWait, but it's possible to make it return some value? like a boolean or something so I can assert it?
  • SeekanDestroy
    SeekanDestroy almost 6 years
    And most important point. Can you check the first method please? it will be possible to create a method with varargs so i can reuse it in different steps? for example: methodtocheckVisibility(element1, elemnt2) methodtocheckVisibility(element3, element4, element5, element6)
  • SeekanDestroy
    SeekanDestroy almost 6 years
    Thanks for the reply!! The first correction work!! But in the second one, as long as visibleWebElements is not a WebDriver class object, it has not a .idDisplayed method. Also, i dont want to use findElement method, cause the point is to use the WebElement object already found (first code section)
  • Shivam Mishra
    Shivam Mishra almost 6 years
    visibleWebElements is a WebElement object and hence you can use the isDisplayed() method on it as I have mentioned in the last code. The findElement thing was just an example to show you the correct way to use it, you are not supposed to use it in your code. Tell me what's the problem with the last code I posted.
  • SeekanDestroy
    SeekanDestroy almost 6 years
    Sorry, you were right. Dunno what happened, the .isDisplayed method was shown in red in my IDE. After copy/paste it, was showing properly. Thanks!