ruby 硒中的 StaleElementReferenceError
StaleElementReferenceError in ruby selenium
我试图自动化 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")
放"Total links are:#{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
每次您重新加载页面(因为您要转到其他页面然后返回,或者因为您只是重新加载页面),您对链接的引用 '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
希望对您有所帮助!
最好的,
费尔南多
我试图自动化 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")
放"Total links are:#{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
每次您重新加载页面(因为您要转到其他页面然后返回,或者因为您只是重新加载页面),您对链接的引用 '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
希望对您有所帮助!
最好的, 费尔南多