Selenium Webdriver 获取 cookie 值 [英] Selenium Webdriver getting a cookie value
问题描述
我正在尝试获取 cookie 值,但不断收到 <Selenium::WebDriver::Driver:0x13a0e0e8 browser=:firefox>
I am trying to get a cookie value but keep getting an error of <Selenium::WebDriver::Driver:0x13a0e0e8 browser=:firefox>
我打电话
@browser.cookie_named("configsession").each do |cookie|
puts cookie[:name]
我做错了什么吗?
推荐答案
使用 cookie 的方法在 Selenium::WebDriver::Options 中定义 - 请参阅 API 文档.
The methods for working with cookies are defined in the Selenium::WebDriver::Options - see the API docs.
要访问这些cookie方法,您需要调用驱动程序的manage
方法:
To access these cookie methods, you need to call the manage
method for the driver:
@browser.manage
要根据名称获取 cookie,您需要执行以下操作:
To get a cookie based on its name, you need to do:
@browser.manage.cookie_named("configsession")
注意 cookie_named
返回一个匹配的 cookie.cookie 值是一个哈希值.因此,您可以通过以下方式获取 cookie 的值:
Note that cookie_named
returns a single cookie that matches. The cookies values are a hash. Therefore, you can get values of the cookie by doing:
cookie = @browser.manage.cookie_named("configsession")
cookie[:name]
#=> "configsession"
如果要获取页面上所有cookies的名称,使用all_cookies
方法:
If you want to get the name of all the cookies on the page, use the all_cookies
method:
driver.manage.all_cookies.each do |cookie|
puts cookie[:name]
end
这篇关于Selenium Webdriver 获取 cookie 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!