如何将Ruby的Selenium WebDriver中的所有cookie保存到txt文件,然后稍后加载?我找不到同时导出和导入的任何答案
在python中,这似乎是一种非常简单的方法,如何在RUBY中做到这一点?
How to save and load cookies using Python + Selenium WebDriver:
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(没有水豚等)
答案 0 :(得分:1)
在测试中,您可以使用宝石show_me_the_cookies(包装器/适配器)
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.
}
没有宝石,您可以直接调用硒驱动器:
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:...)