简体   繁体   中英

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

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

driver.goto url

My cookies not returned in the response.

If i add domain in cookies.

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:

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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