1

super noob here so i apologize in advance for any dumb question...

I am trying to create a script that finds and image by :id, saves it's image URL (named also img src), and then clicks on the image.

so far i've done:

    require 'rubygems'
    require 'selenium-webdriver'

    driver = Selenium::WebDriver.for:firefox
    wait = Selenium::WebDriver::Wait.new(:timeout => 15)

    driver.get 'http://example.com'

    image = wait.until {
        element = driver.find_element(:id,'id_image')
        element if element.displayed?
    }
    #this is the part where i would like to save the IMAGE URL 
    image click.click

I just took the relevant part of the code, i can assure you that so far it all works perfectly but i just can't save the IMAGE URL.

Thank you!

Acolli
  • 33
  • 10

2 Answers2

2

You want the href attribute of the element. In selenium you get attributes using element.attribute('attribute-name')

In your case you would do something like:

href = image.attribute('href')
Mobrockers
  • 2,128
  • 1
  • 16
  • 28
0

I would recommend you to use Capybara since it has several useful selectors.

At this way, you could get the URL attribute with something like this:

href = driver.find_element(:id, 'id_image')[href]

Related question: Capybara: is it possible to get an attribute value from a css string?

Community
  • 1
  • 1
gmfvpereira
  • 2,704
  • 1
  • 14
  • 5