ruby - 如何在 goto 之前设置 cookie?

标签 ruby selenium selenium-webdriver watir

我在 Watir 6.16.5 和 Selenium 3.142.3 中这样做:

b = Watir::Browser.new(driver)
b.cookies.add('foo', 'Hello, world!', domain: 'www.example.com')
b.goto('http://www.example.com')

我想设置 cookie,然后使用 cookie 设置访问网站。但是,我得到:

Selenium::WebDriver::Error::InvalidCookieDomainError: Document is cookie-averse
            WebDriverError@chrome://marionette/content/error.js:179:5
            InvalidCookieDomainError@chrome://marionette/content/error.js:317:5
            GeckoDriver.prototype.addCookie@chrome://marionette/content/driver.js:2641:11

我做错了什么? This可能相关,但解决方法是什么?

最佳答案

您必须在同一个域中才能设置 cookie。来自W3C specs

If the current browsing context’s document element is a cookie-averse Document object, return error with error code invalid cookie domain.

invalid cookie domain 中也有解释

An illegal attempt was made to set a cookie under a different domain than the current page.

解决方法是在域中设置 cookie 后简单地刷新页面

b = Watir::Browser.new(driver)
b.goto('http://www.example.com')
b.cookies.add('foo', 'Hello, world!', domain: 'www.example.com')
b.refresh

关于ruby - 如何在 goto 之前设置 cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57303969/

相关文章:

selenium - 使用 Selenium 时 JsonWireProtocol 机制与 W3C Living Document 中新标准的区别

ruby - 在 Ruby 中,为什么在 each 语句中使用 puts 函数时三元运算符不起作用?

javascript - 如何使用 phantomjs 和 php-webdriver 跟踪页面重定向并持续设置常量浏览器用户代理

ruby-on-rails - rails : Many to many relation to self

testing - Jmeter 与 Selenium

java - 无法打印移动 View 滚动中显示的所有元素

javascript - 通过 Selenium 和 Python 通过 WebDriver 实例调用 execute_script() 方法时 arguments[0] 是什么?

java - 如何从 Selenium Webdriver 的属性文件中选择随机值?

ruby-on-rails - Rails 应用程序显示 'index of/cms' 而不是 Rails

ruby - 在 ruby​​ 中使用 matplotlib.image 是否有等效项