首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Ruby + Selenium WebDriver保存和加载cookies

问如何使用Ruby + Selenium WebDriver保存和加载cookies
EN

Stack Overflow用户
提问于 2019-05-28 17:11:46
回答 1查看 1.3K关注 0票数 1

如何将Ruby的Selenium WebDriver中的所有cookie保存到一个txt文件中,然后再加载它们?我找不到同时导出和导入的答案

在python中看起来很容易做到这一点,如何在RUBY中做到这一点呢?

How to save and load cookies using Python + Selenium WebDriver

代码语言:javascript
复制
import pickle
import selenium.webdriver 

driver = selenium.webdriver.Firefox()
driver.get("http://www.google.com")
pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))

编辑:我只使用Selenium WebDriver (没有水豚等)

EN

回答 1

Stack Overflow用户

发布于 2019-05-28 17:39:31

在测试中,您可以使用gem show_me_the_cookies (包装器/适配器)

代码语言:javascript
复制
cookies = show_me_the_cookies # => [{:name, :domain, :value, :expires, :path, :secure}]
# here you can write them as you like
# and then load and
cookies.each{|c|
  create_cookie(c[:name], c[:value], path: c[:path], domain: c[:domain]) # etc.
}

没有gem,您可以直接调用selenium驱动程序:

代码语言:javascript
复制
driver = Capybara.current_session.driver # or get your selenium driver other way if not using capybara
cookies = driver.browser.manage.all_cookies
# be sure that you've visited a page in your app, selenium cannot create cookies at `about:blank`
driver.browser.manage.add_cookie(name: ..., value:...)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56338704

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档