I can't seem to figure out the best way to find multiple elements on a page.
It always finds one but what if I'm testing to see how many particular elements are present after some kind of action?
findElement() // always returns 1 element
findElements(By.className('someClass')) // always returns 1 element
When the find element method is called on the driver instance, it returns a reference to the first element in the DOM that matches with the provided locator. This value can be stored and used for future element actions.
We can locate and identify multiple elements at the same time in Selenium. There are various strategies to locate the elements. The different ways of locating elements are listed below − find_elements_by_xpath – This method returns all the elements with matching xpath in the argument in form a list.
Find the two repeating elements in a given array. You are given an array of n+2 elements. All elements of the array are in range 1 to n. And all elements occur once except two numbers which occur twice. Find the two repeating numbers. For example, array = {4, 2, 4, 5, 2, 3, 1} and n = 5.
Since, the size of the array is n+1 and elements ranges from 1 to n then it is confirmed that there will be at least one repeating element. A simple solution is to create a count array and store counts of all elements. As soon as we encounter an element with count more than 1, we return it.
In case someone looks for this in the future, I got it to work:
findElements(By.className("someclass")).then(function(elements_arr){
console.log(elements_arr.length);
});
According to their source code, findElements returns a promise
Webdriverjs API
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With