1

I have around 40-50 elements to check in a page using Selenium WebDriver and I am using the code below to check each element. Can someone please tell me what is the best way to do such testing if I have many elements in one page. Boolean. What I am doing now is:

isPresentquote_graph_displayed = driver.findElements(element1).size() > 0; 
isPresentquote_graph_displayed = driver.findElements(element2).size() > 0; 
isPresentquote_graph_displayed = driver.findElements(element3).size() > 0; 
isPresentquote_graph_displayed = driver.findElements(element4).size() > 0;

Like this I am writing each of the 40 to 50 elements and checking if the boolean value is true or false. I was wondering if there is any better way to do this?

  • I would create an array with all the elements, then loop through them. This is also assuming you want to check the same value for all. If you wanted to check true for some and false for others, you'd want to assign the element and value in the array, then you can pass both into the check. – Swagin9 Mar 9 '17 at 17:23
  • 1
    yea that make sense. thanks for the suggestion. :) – sarath Mar 9 '17 at 17:38
  • Sad status of questions being asked here: using a loop to perform an action repeatedly is a good suggestion. Why it was not obvious for OP? :-( – Peter M. Mar 9 '17 at 18:11
0

I would create an array with all the elements, then loop through them. This is also assuming you want to check the same value for all. If you wanted to check true for some and false for others, you'd want to assign the element and value in the array, then you can pass both into the check.

1

It is better to create the list and iterate through the each and every element in the list and find that if it is present

list[] ele = new list[](driver.findElement(""));
foreach(var elementpresent in ele)
{
    if(elementpresent.isPresent) //or elementpresent.isDisplayed
    {
        return 1;
    }
    else
    {
        return 0:
    }
}

I hope this works

New contributor
Pavan N is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.