硒的webdriver得到一个cookie的值 [英] Selenium Webdriver getting a cookie value

查看:147
本文介绍了硒的webdriver得到一个cookie的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图得到一个cookie的值,但不断收到&LT的一个错误;硒::的webdriver ::驱动程序:0x13a0e0e8浏览器=:火狐>

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]

是不是我,我做错了?

is there something I i'm doing wrong?

推荐答案

与饼干工作方法在硒:: ::的webdriver选项中定义 - 见的 API文档

The methods for working with cookies are defined in the Selenium::WebDriver::Options - see the API docs.

要访问这些cookie的方法,你需要调用管理​​驱动程序方法:

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的值:

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"

如果你想获得页面上的所有饼干的名称,使用 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

这篇关于硒的webdriver得到一个cookie的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆