Patrick Kinsella
Patrick Kinsella

Reputation: 25

How to find elements in nested divs with Selenium WebDriver

I'm trying to find apps on the Google Play store and having trouble locating elements that are nested inside other elements. I've seen some examples in Java and not sure I'm implementing the Ruby correctly. I want to select an outer div container, then select divs within that, until I get to the app title, and print it.

I've read the documentation for Selenium WebDriver and tried to read some of the rubygem documentation, and also done Google searches for finding nested div elements with WebDriver. This is my first Selenium WebDriver project.

i = 0

app_cards = driver.find_elements(:class_name, "card-content")
app_details = app_cards.find_elements(:class_name, "details")
app_titles = app_details.find_elements(:class_name, "title")

app_titles.each do |app_title|
    puts "App #{i}: " + app_title.attribute("title")
    i = i + 1
end

I expected the script to print all the app results on a page of the Google Play store, and I get

main.rb:17:in `<main>': undefined method `find_elements' for #<Array:0x00005594417b5f68> (NoMethodError)

Upvotes: 1

Views: 717

Answers (1)

Rajagopalan
Rajagopalan

Reputation: 6064

you can do something like this to print all the titles

require 'selenium-webdriver'
driver=Selenium::WebDriver.for :firefox
driver.manage.timeouts.implicit_wait = 20
driver.navigate.to('https://play.google.com/store/apps')
driver.find_elements(css: '.details > .title').to_enum.with_index(1) do |element,index|
  puts "App #{index}: " +element.attribute('title')
end

Upvotes: 2

Related Questions