Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

Using Selenium and Cucumber, I'm trying to set up a method to store WebElements through arguments. 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);

    }

}

I am getting this error on .add(elements):

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

Also, here is the second method to check for visibility:

public void estaVisible(WebDriver(tried WebElement as well) visibleWebElements) {

    boolean esvisible =  driver.findElement(visibleWebElements).isDisplayed();

    Assert.assertTrue(esvisible);

    return esvisible;

}

I am 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 objects, but aren't the objects found by WebDriver stored as WebElements?

Could someone put some light on this question please? Thanks in advance.  

1 Answer

0 votes
by (62.9k points)

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 example, if you want to locate an element by XPath, the following 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 esvisiblevisibleWebElements.isDisplayed();

    Assert.assertTrue(esvisible);

    return esvisible;

}

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s automation testing training

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...