Avinash Duggirala
Avinash Duggirala

Reputation: 371

How to retrieve values from a rect html tag using watir

Is there any way to retrieve values from the rect tag using Watir. I can give an example URL. There are rectangular bars on the lower part of the page. Please find it

http://www.healthgrades.com/hospital-directory/california-ca-los-angeles/good-samaritan-hospital-hgste2618d46050471?#Readmission

I have seen couple of sources but I have not got any way to get those details. Can any one help me here?

Upvotes: 0

Views: 346

Answers (3)

Jeff Price
Jeff Price

Reputation: 3229

Yes, you can access it as a generic element (example only, not applicable to your code). The data element you are looking for is highcharts-data-labels.

values = browser.element(:css => "g.highcharts-data-labels")

You could also access the element using an xpath selector and can get this in Chrome by inspecting the element and asking for the xpath selector

//*[@id="highcharts-2"]/svg/g[4]/g[2]/text

Good luck!

Upvotes: 1

Chuck van der Linden
Chuck van der Linden

Reputation: 6660

You can access that element in one of the two ways that @jeff-price pointed out, or you could extend watir to add support for the tag.

This is not included by default since that stuff is not part of the HTML5 standard proper. With what essentially amount to 'custom' tags like this there are of course an unlimited number of potential custom tags. it is impossible to add support for all of them.

OTOH it is very easy for you to include a small snippet of code in your project that would add support for the tag to watir, which if you know your project is using it a lot, makes perfect sense.

# extend watir to allow support for custom elements not expressly defined in HTML spec

module Watir
  module Container
    def g(*args)
      G.new(self, extract_selector(args).merge(:tag_name => "g"))
    end

    def gs(*args)
      GCollection.new(self, extract_selector(args).merge(:tag_name => "g"))
    end
  end

  class G < Element
  end

  class GCollection < ElementCollection
    def element_class
      G
    end
  end
end

depending on what framework etc you are using, you can either put that into a file and require the file early in your test code, or put it into a file in a directory that is automatically loaded..

e.g. I use Cucumber, so I created a file named custom_element_support.rb with that code in it and put it into the features/support directory where cucumber will automagically load it as it starts up

Upvotes: 0

alejandroj1234
alejandroj1234

Reputation: 141

you can use xpaths to select the desired rectangle. Each rectangle on the link you provided has a unique xpath. Your code could be something like this:

browser.element(:xpath, "//*[@id='highcharts-0']/svg/g[7]/g/rect[1]").flash 

Upvotes: 0

Related Questions