如何在水豚中使用自己的cookie?

时间:2016-04-08 18:42:01

标签: ruby selenium cookies capybara

我试图(ab)使用capybara web测试框架来自动化github上的某些任务,这些任务无法通过github API访问,并且要求我登录并单击按钮发送AJAX请求。

由于capybara / selenium是一个测试框架,它有助于创建一个没有cookie的临时会话。我想阻止它这样做,或者我想知道如何将我的cookie存储加载到它创建的浏览器会话中。

我试图做的就是:

#!/usr/bin/env ruby

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :chrome
driver.navigate.to "https://github.com"

或者这个:

#!/usr/bin/env ruby

require 'capybara'

Capybara.register_driver :selenium do |app|
  Capybara::Selenium::Driver.new(app, :browser => :chrome)
end

session = Capybara::Session.new(:selenium)
session.visit "https://www.github.com"

在这两种情况下,我都会获得您在浏览器中看作已注销用户或隐身模式的github.com登录页面。我想登录我的登录页面,就像我自己启动了一个网页浏览器并导航到该网址一样。

由于我在github上设置了2FA,这使得从github登陆页面自动登录过程有点烦人,所以我想避免自动登录到github。我想要自动化的任务不需要通过2FA重新进行身份验证。

解答:

对于MacOSX + Ruby + Selenium,这有效:

#!/usr/bin/env ruby

require 'selenium-webdriver'

caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"debuggerAddress" => "127.0.0.1:20480"}, detach: false)
driver = Selenium::WebDriver.for :chrome, :desired_capabilities => caps
driver.navigate.to "https://github.com"

然后点亮铬:

% /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --user-data-dir=/Users/lamont/Library/Application\ Support/Google/Chrome --profile-directory=Default --remote-debugging-port=20480

显然这些路径需要调整,因为它们以OSX为中心并且有我的homedir。

在selenium-webdriver gem中还有一个针对ruby的错误,它会插入'分离'使用' debuggerAddress'

进行斗争的选项
/Users/lamont/.rvm/gems/ruby-2.2.4/gems/selenium-webdriver-2.53.0/lib/selenium/webdriver/remote/response.rb:70:in `assert_ok': unknown error: cannot parse capability: chromeOptions (Selenium::WebDriver::Error::UnknownError)
from unknown error: unrecognized chrome option: detach

可以编辑lib/selenium/webdriver/chrome/bridge.rb文件以将其作为快速黑客攻击:

      chrome_options['binary']                   = Chrome.path if Chrome.path
      chrome_options['nativeEvents']             = true if native_events
      chrome_options['verbose']                  = true if verbose
      #chrome_options['detach']                   = detach.nil? || !!detach
      chrome_options['noWebsiteTestingDefaults'] = true if no_website_testing_defaults
      chrome_options['prefs']                    = prefs if prefs

2 个答案:

答案 0 :(得分:1)

要在Ruby中实现类似的功能,请查看过去的this page。感谢lamont在评论中告诉我。

您可以使用特定的Chrome个人资料启动Chrome。我不确定ruby实现会是什么样子,但在python中看起来像是:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions

options = ChromeOptions()
# more on this line here later.
options.add_experimental_option('debuggerAddress', '127.0.0.1:7878')
driver = webdriver.Chrome(chrome_options=otpions)

为了实现这一目标,您需要做一些事情。

  

使用这些命令行参数从终端/命令提示符手动启动chrome   --user-data-dir=/path/to/any/custom/directory/home/user/Desktop/Chromedir --profile-directory="Profile 1" --remote-debugging-port=7878

     
      
  1. 确保“配置文件1”已存在于同一个--user-data-dir中(确保用户配置文件1具有必要的chrome:// components /   运行任何需要这些组件的应用程序)
  2.   
  3. 您可以使用任何免费端口代替7878   验证http://localhost:7878是否正在运行并返回值。
  4.   

这应该使用"Profile 1"配置文件手动启动chrome,只要它已登录到相关网站,只要您按照这些说明运行测试,它就会像普通用户一样保持登录状态

当它弹出时,我用这个来写一个快速的netflix bot that clicks the "continue playing" button,这是我发现DRM内容的唯一方法。但是它保留了登录的cookie,并且还使用配置文件设置的任何组件启动chrome。

在使用不同的方法之前,我尝试使用特定的配置文件启动chrome,但这是真正强制它按照我想要的方式工作的唯一方法。

编辑:虽然我不知道它们的工作情况,但也有保存cookie信息的方法。查看this link了解详情,因为我的解决方案可能不是最佳解决方案,即使它有效。

答案 1 :(得分:0)

show_me_the_cookies gem提供跨驱动程序cookie操作,可以让您添加新的cookie。使用selenium时要注意的一件事是,您需要先访问域名才能为其创建Cookie,因此您需要执行类似

的操作
visit "https://www.github.com"
create_cookie(...)
visit "https://www.github.com"

让它工作 - 首先访问只是将浏览器/驱动程序置于可以创建cookie的状态,第二次访问实际上转到设置了cookie的页面。

相关问题