问题描述
我很惊讶,似乎没有多少讨论这个。
I'm surprised there didn't seem to be much discussion on this.
在我可以从浏览器轻松读取整个cookie jar,将其存储到一个文件,并将其加载到以后的会话/运行,然后再加载该网站的页面。
in Mechanize I can easily read an entire cookie jar from the browser, store it to a file, and load it in to a later session/run before loading that website's pages again.
如何使用 watir-webdriver 做同样的事?
UPDATE
现在使用我看到了新方法 browser.cookies.to_hash
这将把这个问题变成如何实现 .from_hash 或类似的加载器使用例如 .clear
和 .add
?
Now with 0.5.2 I do see new methods browser.cookies.to_hash
which would turn this question into "How to implement .from_hash or similar loader using eg. .clear
and .add
?"
但我会特别热衷加载和保存所有使用以前版本(0.4.1)的cookies,我的服务器可能会停留一段时间。通过Selenium驱动程序吗?
However I'd be especially keen on loading and saving all cookies using previous versions (0.4.1) which my servers are likely to be stuck with for a while. Via the Selenium driver maybe?
推荐答案
browser = Watir::Browser.new :firefox
browser.goto 'http://google.com'
# save cookies
saved_cookies = browser.cookies.to_a
# clear and get new cookies
browser.cookies.clear
browser.goto 'http://google.com'
# set new cookies
browser.cookies.clear
saved_cookies.each do |saved_cookie|
browser.cookies.add(saved_cookie[:name], saved_cookie[:value])
end
这篇关于watir-webdriver饼干罐保存和加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!