Question

I'm a bit further along in converting some sample test/specs from Watir to Selenium. After my last question here and suggested response, I began using Selenium 2.0 with WebDriver instead of Selenium 1.

The example in question deals with gathering all links within a table into an array -- that part is complete. However, once the links are in the array, the only meaningful way that I can interact with them appears to be .text. Using @driver.navigate.to Array[1] gives a URL format error in the browser, and link.href or .src are not valid options.

The Watir implementation gathered these links (pages added by users via CMS), stored them in an array and then visited each page one by one, submitting a lead form. I believe I could get this to work using Selenium and revisiting the "home" page that contains all of the links between lead form submissions, but that could mean hundreds of extra page loads, cached or not.

The code so far: ' @countries = Array.new

@browser.navigate.to "http://www.testingdomain{$env}.com/global"  
@browser.find_elements(:xpath, "//table[@class='global-list']//a").each do |link|      
  @countries << [link.text, link.href]  ## The original WATIR line that needs an update
end #links  

@countries.uniq! #DEBUG for false CMS content'

The closest item I could find in the selenium-webdriver documentation was the (string).attribute method, but again, am unsure of what attributes

Was it helpful?

Solution

I was not sure of the format for use with the attribute method, but after some experimenting I was able to move past this step.

@countries = Array.new

@browser.navigate.to "http://www.testingdomain{$env}.com/global"

@browser.find_elements(:xpath, "//table[@class='global-list']//a").each do |link|
text = link.attribute("text")
href = link.attribute("href")
@countries << [text, href]
end #links
`@countries.uniq! #DEBUG for false CMS content

OTHER TIPS

Looks like you found your answer to your question on your own.

Indeed, element.attribute allows you to pull any HTML attribute a tag could possibly have. Thus, since you were wanting to pull the URL of the element, you used element.attribute('href') to return the element's href="" attribute. The same can be done for any other attributes, including class, id, style, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top