简体   繁体   中英

How can I generate a cookie file from Watir cookies

I am trying to download a file from Watir but I don't want to use the loop sleep approach.

I would rather at the last moment of the interaction, recreate the session Watir has on the webpage and use another library, for example Typhoeus.

Typhoeus uses curl and can use cookies from a file, nonetheless, Watir generates a Hash Array, and if I ask to save it, it saves it as a YAML file.

Is there a faster way to convert it?

Another article in StackOverflow said that curl uses Mozilla style cookie files .

So, if your Watir instance is browser and the file to which you are going to write to is file you can do

browser.cookies.to_a.each do |ch| 
  terms = []
  terms << ch[:domain]
  terms << ch[:same_site].nil? ? 'FALSE' : 'TRUE'
  terms << ch[:path]
  terms << ch[:secure] ? 'TRUE' : 'FALSE'
  terms << ch[:expires].to_i.to_s
  terms << ch[:name]
  terms << ch[:value]
  file.puts terms.join("\t")
end

Then you can tell Typhoeus to use the contents of file to keep navigating using the same cookies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM