【问题标题】:How to save and load cookies using Ruby + Selenium WebDriver如何使用 Ruby + Selenium WebDriver 保存和加载 cookie
【发布时间】:2019-05-28 18:23:13
【问题描述】:

如何将 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(没有 capybara 等)

【问题讨论】:

    标签: ruby-on-rails ruby selenium-webdriver


    【解决方案1】:

    在测试中,您可以使用 gem 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.
    }
    

    没有 gems 你可以直接调用 selenium 驱动:

    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:...)
    

    【讨论】:

    • 我只使用 selenium webdriver
    猜你喜欢
    • 2013-02-10
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-07
    相关资源
    最近更新 更多