Ruby 使用watir webdriver或selenium设置cookie

Ruby 使用watir webdriver或selenium设置cookie,ruby,cookies,selenium-webdriver,watir-webdriver,Ruby,Cookies,Selenium Webdriver,Watir Webdriver,创建驱动程序:- profile = Selenium::WebDriver::Firefox::Profile.new driver = Watir::Browser.new :firefox, profile: profile url= 'http://www.example.com' 添加cookie:- driver.cookies.add("test","1",{expires: 10.days.from_now}) driver.goto url 我的cookies没有在

创建驱动程序:-

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

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

url= 'http://www.example.com'
添加cookie:-

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

driver.goto url
我的cookies没有在响应中返回

如果我在cookies中添加域

driver.cookies.add("test","1",{expires: 10.days.from_now, domain: 'example.com'})
其说法错误如下:-

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

谁能帮我解决这个问题

您必须在为其设置cookie的域上,因此请尝试:

driver.goto url
driver.cookies.add("test","1",{expires: 10.days.from_now})
现在,当您在同一个域上执行以下操作时,它应该会显示:

driver.cookies.to_a

这是假设您需要有效的支持/时间(以使到期有效)。

我尝试了您的解决方案。但仍然出现错误,因为“您只能在html文档上设置Cookie”页面是否已加载?如果浏览器仍处于“大约:空白”状态,则可能会出现此错误。您能告诉我您使用的url是什么吗?如果没有,您在使用example.com时是否也会遇到问题中的错误?如果使用您的示例时一切正常(这对我来说很好),那么问题在于url。是的,它与example.com一起使用,但与我的url不一起使用。