I'm surprised there didn't seem to be much discussion on this.
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.
How can one do the same with watir-webdriver?
UPDATE
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
?"
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With