aoxford
aoxford

Reputation: 25

Finding nth element by class with Selenium Webdriver

I'm new to Ruby and Selenium Webdriver and was wondering if anyone knew how to find the nth element when searching by class. I was able to find an answer for the java version but I'm not sure what the syntax is for Selenium.

here is the documentation I've been using: http://code.google.com/p/selenium/wiki/RubyBindings

Here is my specific problem: I am writing a program that crawls LinkedIn and views profiles found in the "People similar to ...." section. I found that the first similar person can be found with

element = browser.find_element(:class, "discovery-photo")
element.click

What I want to be able to do is to choose the element of the second (or third, or fourth) person in that least - so the third element with class = "discovery-photo". The way it is currently set-up it only returns the first one.

One more thing - I was able to find that the element number can be determined easily with data-li-index but I have no idea how I would use selenium webdriver to find that. I tried find_element(:data-li-index, "1") but that threw an error.

Upvotes: 2

Views: 1273

Answers (1)

Purus
Purus

Reputation: 5829

Use the find_elements method, which will return all elements which match the particular class you specify.

browser.find_elements(:class, "discovery-photo")

This will return a list of elements which you can loop through.

Upvotes: 2

Related Questions