使用Ruby的selenium-webdriver创建一个特定于域的".site.com"cookie

Ale*_* T. 2 ruby cookies selenium

我正在尝试使用域,而不是主机或整个网站创建cookie.

我现在有这个代码

driver.manage.add_cookie(:name => 'test', :value => 'testvalue', :path => '/', :secure => false)
Run Code Online (Sandbox Code Playgroud)

我想要这样的东西

name=test
value=testvalue
domain=.site.com
path=/
Run Code Online (Sandbox Code Playgroud)

我在firefox cookie对话框中得到了这样的结果

在此输入图像描述

虽然我想要这样的东西

在此输入图像描述

你可以看到Host:我的情况是空的,在另一种情况下,它被替换为Domain: ,这是我想要实现的,设置一个cookie域到.mydomain.com

我希望JavaScript能够实现这一点,以便能够读取特定于域的cookie,因为它无法读取当前域范围之外的内容.

fal*_*tru 5

试试以下:

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :firefox
driver.get('http://eu.httpbin.org') # <-- required.
driver.manage.add_cookie(name: 'test', value: 'testvalue', path: '/', domain: '.httpbin.org')
driver.get('http://eu.httpbin.org/cookies') # eu.httpbin.org
puts driver.page_source
# => ...
# {
#   "cookies": {
#     "test": "testvalue"
#   }
# }
# ...
driver.get('http://httpbin.org/cookies') # httpbin.org
puts driver.page_source
# => ...
# {
#   "cookies": {
#     "test": "testvalue"
#   }
# }
# ...
Run Code Online (Sandbox Code Playgroud)

注意:在添加cookie之前,您必须转到相同的域页面(html页面).