ruby selenium中的StaleElementReferenceError

时间:2016-02-23 17:58:40

标签: ruby selenium staleobjectstateexception

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

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

@ driver.manage.window.maximize

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

开始

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

确保

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

端   链接= box.find_elements(:TAG_NAME, “A”)

将“总链接为:#{links.size}”

links.each do | 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

1 个答案:

答案 0 :(得分:1)

每次重新加载页面时(因为您要转到其他页面然后返回或因为您只是简单地重新加载页面)您对链接的引用' links = box.find_elements(:tag_name,&#34 ;一个")'迷路了。

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

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

我希望这可以帮到你!

最佳, 费尔南多