Selenium 2.0 Webdriver & Ruby, link element methods other than .text? Navigate.to links in array?

ⅰ亾dé卋堺 提交于 2019-12-07 18:54:54

问题


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


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/4136636/selenium-2-0-webdriver-ruby-link-element-methods-other-than-text-navigate-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!