【问题标题】:StaleElementReferenceError in ruby selenium红宝石硒中的StaleElementReferenceError
【发布时间】:2016-02-23 17:58:39
【问题描述】:

我试图自动化 rediff.com 。我从一页转到另一页,但是当我回来时,我得到了 staleException 。我尝试了很多但无法修复它。 我也附上了代码 sn-p。任何帮助将不胜感激。

@driver.get "http://shopping.rediff.com/?sc_cid=inhome_icon"

@driver.manage.window.maximize

wait = Selenium::WebDriver::Wait.new(:timeout => 10) # 秒

开始

element = wait.until { @driver.find_element(:xpath,".//*[@id='popular_cat']") }

确保

box=@driver.find_element(:xpath,".//*[@id='popular_cat']")

结束 链接=box.find_elements(:tag_name,"a")

puts "总链接数:#{links.size}"

links.each 做|i|

  puts "--------------------"
  puts "Value of all links is:#{i.text}"
  i.click
  puts "Title of page is :#{@driver.title}"
  @driver.get "http://shopping.rediff.com/?sc_cid=inhome_icon"
  box=@driver.find_element(:xpath,".//*[@id='popular_cat']")
  links=box.find_elements(:tag_name,"a")

end

【问题讨论】:

  • 我怀疑您使用的是 Firefox?如果是这样,那将解释错误。

标签: ruby selenium staleobjectstateexception


【解决方案1】:

每次您重新加载页面时(因为您要转到其他页面然后返回或因为您只是重新加载了页面)您对链接 'links=box.find_elements(:tag_name,"a")' 的引用是丢了。

我建议进行一些更改以解决此问题(可能不是最佳解决方案)

links = box.find_elements(:tag_name,"a").size links_counter = 0 while links_counter < links box = @driver.find_element(:xpath,".//*[@id='popular_cat']") current_link = box.find_elements(:tag_name,"a")[links_counter] links_counter += 1 puts "--------------------" puts "Value of all links is:#{current_link.text}" current_link.click puts "Title of page is :#{@driver.title}" @driver.get "http://shopping.rediff.com/?sc_cid=inhome_icon" end

希望对你有帮助!

最好, 费尔南多

【讨论】:

  • 感谢您的快速回复。您让我很开心,并 +1 寻求帮助。您能否分享我可以学习 ruby​​ 的任何链接?
  • 当然,你可以去 codecademy 网站或 codeschool,他们有很好的教程和课程。也 [链接] (railscasts.com) 了解更多关于 rails 的 ruby​​ 内容(非常酷的 tutos)。抱歉耽搁了:D
猜你喜欢
  • 1970-01-01
  • 2016-04-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-09
  • 2012-09-21
  • 2019-04-27
  • 1970-01-01
  • 2019-08-09
相关资源
最近更新 更多