Galet
Galet

Reputation: 6309

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?

Upvotes: 4

Views: 3232

Answers (1)

Wilco van Esch
Wilco van Esch

Reputation: 457

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

Upvotes: 5

Related Questions