简体   繁体   English

如何从 Watir cookies 生成 cookie 文件

[英]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.我正在尝试从 Watir 下载文件,但我不想使用loop sleep方法。

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.我宁愿在交互的最后一刻,重新创建 Watir 在网页上的 session 并使用另一个库,例如 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. 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 . StackOverflow 中的另一篇文章说 curl 使用Mozilla 风格的 cookie 文件

So, if your Watir instance is browser and the file to which you are going to write to is file you can do因此,如果您的 Watir 实例是browser并且您要写入的文件是file ,您可以执行

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.然后你可以告诉 Typhoeus 使用file的内容来继续使用相同的 cookies 导航。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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