2

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

So should I set an array equal to it? array = browser.find_elements(:class, "discovery-photo") And then select the correct element number in the array?
Yes. It would be an array of WebElements.
Really useful answer - this helped me out. I used: array = browser.find_elements(:class, "discovery-photo") array[9].click()

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.