I am using this code, to grab the
inner_html
weather = browser.span(:class => "_Xbe kno-fv").inner_html
puts weather
inner_html
weather = browser.span(:class => "_Xbe kno-fv").text
puts weather
Examples:
| Search |
| Bangalore | 741 km²
Examples:
| Search |
| Bangalore | 17°C, Wind E at 3 km/h, 69% Humidity
The element that you are targeting is contained within a <div>
with a unique data attribute. I'd advise using that approach:
require 'watir'
b = Watir::Browser.new :chrome
b.goto "https://www.google.com/?gfe_rd=cr&ei=w7-YWN75OsyAoAPK_oDYBQ#q=bangalore"
puts b.div(:data_attrid => "kc:/location/citytown:current weather").text
#=> Weather: 59°F (15°C), Wind S at 2 mph (3 km/h), 83% Humidity
For reference--as @JustinKo demonstrates--watir
can use data attributes as locators with some minor tweaks.
Sidenote: running automated searches against Google may be a violation of their Terms of Service, and you may find yourself blocked. You should investigate if you can use one of their many APIs to harvest this data.