我在 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/