0

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

@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
4

1 回答 1

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

我希望这可以帮助你!

最佳,费尔南多

于 2016-02-23T19:09:52.547 回答