1

I have a collection of media item on a web page and all media items have the same class. The elements do not have IDs. I do not want use XPath selectors. When I use class name, it will pick only the first item, but I want click on the second item.

Currently I have:

WebElement ele = wd.findElement(By.className("vfmThumbnail"));
ele.click();
3
  • Can you post your code please?
    – FDM
    Commented Apr 23, 2015 at 6:33
  • i am using this simple code WebElement ele = wd.findElement(By.className("vfmThumbnail")); ele.click(); Commented Apr 23, 2015 at 6:35
  • Can you post the html of the elements you're looking at? Also, can you explain what it is about the second element that you're interested in, and how you can tell it apart from the others? Commented Apr 24, 2015 at 9:23

2 Answers 2

4

Try to use driver.findElements instead. It returns a list of all WebElements. All means also the not visible elements.

Here some code to iterate thru the list and only do something with the displayed items.

List<WebElement> elements = driver.findElements(By.className("vfmThumbnail"));
java.util.Iterator<WebElement> i = elements.iterator();
while(i.hasNext()) {
    WebElement element = i.next();
    if (element.isDisplayed()) {
      // Do something with the element
    }
} 
1

If you want all elements that match your selector, use findElements. This will return a list which you can iterate through.

List rows = wd.findElements(By.className("vfmThumbnail"));
1
  • yes thanks FDM i used this but for click on second media i used this rows.get(2).click(); then its working and now its too simple. Commented Apr 23, 2015 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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