简体   繁体   English

使用watir webdriver或selenium设置cookie

[英]Set cookie using watir webdriver or selenium

Creation of driver:- 创建驱动程序: -

profile = Selenium::WebDriver::Firefox::Profile.new 

driver = Watir::Browser.new :firefox, profile: profile

url= 'http://www.example.com'

Adding cookies:- 添加cookies: -

driver.cookies.add("test","1",{expires: 10.days.from_now})

driver.goto url

My cookies not returned in the response. 我的cookie没有在回复中返回。

If i add domain in cookies. 如果我在cookie中添加域名。

driver.cookies.add("test","1",{expires: 10.days.from_now, domain: 'example.com'})

its saying errors like below:- 它说的错误如下: -

Selenium::WebDriver::Error::InvalidCookieDomainError: You may only set cookies for the current domain

Can anyone help me to solve this? 任何人都可以帮我解决这个问题吗?

You have to be on the domain you're setting the cookie for, so try: 你必须在你正在设置cookie的域名上,所以试试:

driver.goto url
driver.cookies.add("test","1",{expires: 10.days.from_now})

Now it should show up when on the same domain you do: 现在它应该出现在你所在的同一个域上:

driver.cookies.to_a

That is assuming you've required active_support/time (to make the expiry work). 假设您需要active_support / time(以使到期工作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM