0

I am trying to scrape a website and strip two different things and print them together. The title and description of PlayStation Trophies.

require 'selenium-webdriver'

  driver = Selenium::WebDriver.for(:chrome, options: options)

  driver.get('https://www.playstationtrophies.org/game/far-cry-4/trophies/')

  puts driver.title

  trophies = driver.find_elements(:class, 'link_ach')
  description = driver.find_elements(:class, 'ac3') 

  trophies.each do |trophy|
    description.each do |desc|
      puts trophy,desc.text.strip
     end
  driver.quit
end

I can strip them both individually but when trying to put them together it goes horribly wrong.

1
  • 2
    Can you describe what you mean by horribly wrong and what the expected output is? Commented Mar 2, 2019 at 15:04

2 Answers 2

1

I'm not entirely sure what you mean by 'horribly wrong' but my best guess based on running what you have is that for trophy you're actually printing the inspection of the variable trophy to the screen instead of the string value. So you're getting lots of:

#<Selenium::WebDriver::Element:0x00007ff4b60e5eb8>

I think you'll be happier with the output if you print the text values of both Element's:

puts "#{trophy.text.strip}\n #{desc.text.strip}"

If this is indeed the issue you're seeing you may want to review to_s vs inspect in ruby.

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

3 Comments

Thanks. That makes sense. Although I am seeing a disconnect between the two. How do I print the associated trophy with the description underneath?
puts "#{...}\n #{...}" where \n is just a newline.
added the newline for completeness
0

Iteration over trophies and description is very confusing here. You have two separate collections/arrays, trophies and description. Then you have used a nested loop and quit driver inside the loop. So it will display all the descriptions under 1st trophy.

If you want to display each description under associated trophy then you can do this:

puts driver.title

trophies = driver.find_elements(:class, 'link_ach')
descriptions = driver.find_elements(:class, 'ac3')
data_sets = trophies.zip(descriptions)

datasets.each do |trophy, description|
    puts trophy.text.strip, description.text.strip
end

driver.quit

Then you can customize your display text in puts as your needs.

Comments

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.