Selenium 2.0 Webdriver 和 Selenium 2.0 Webdriver Ruby,除了 .text 之外的链接元素方法? Navigate.to 数组中的链接?
我在将一些示例测试/规范从 Watir 转换为 Selenium 方面取得了进一步的进展。在我提出最后一个问题并建议回复之后,我开始使用带有 WebDriver 的 Selenium 2.0 而不是 Selenium 1。
所讨论的示例涉及将表中的所有链接收集到一个数组中——该部分已完成。但是,一旦链接位于数组中,我可以与它们交互的唯一有意义的方式似乎是 .text。使用 @driver.navigate.to Array[1] 会在浏览器中出现 URL 格式错误,并且 link.href 或 .src 不是有效选项。
Watir 实现收集这些链接(用户通过 CMS 添加的页面),将它们存储在数组中,然后逐一访问每个页面,提交潜在客户表单。我相信我可以使用 Selenium 来实现这一点,并重新访问包含潜在客户表单提交之间的所有链接的“主页”页面,但这可能意味着数百个额外的页面加载,无论是否缓存。
到目前为止的代码: ' @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'
我在 selenium-webdriver 文档中找到的最接近的项目是 (string).attribute 方法,但同样,我不确定哪些属性
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定属性方法使用的格式,但经过一些实验,我能够跳过这一步。
@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")
@国家<< [文本,href]
结束#links
<代码>
`@countries.uniq! #DEBUG 错误的 CMS 内容
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
看来您自己找到了问题的答案。
事实上,element.attribute 允许您提取标签可能具有的任何 HTML 属性。因此,由于您想要提取元素的 URL,因此使用
element.attribute('href')
返回元素的 href="" 属性。对于任何其他属性,包括类、id、样式等,都可以执行相同的操作。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.