how to set/add cookies in webdriver with domain

15,503 views
Skip to first unread message

Rajiv Nanduani

unread,
Jul 4, 2012, 3:54:02 AM7/4/12
to seleniu...@googlegroups.com

hi i have to add/set cookies using webdriver.But i am getting error in eclipse. Please let me know what is the correct syntax to add cookies in web driver



NAME betaaccess
VALUE true
DOMAIN abc.com
PATH /
EXPIRES 7/6/2012 10:26:47 AM


i have tried below this but getting syntax error

______________________________________

        driver.manage().addCookie(new Cookie("foo", "bar", "www.google.com", "/", null));
___________________________________________
    Cookie cookie = new Cookie("betaaccess", "true");
    cookie.setDomain("abc.com");
    cookie.setPath("/");
    driver.manage().addCookie(cookie );

____________________


driver.manage().addCookie( 'path=/; domain=.example.com');



Regards

RAJIV KUMAR NANDVANI

http://rajivkumarnandvani.wordpress.com
http://testeverythingqtp.blogspot.com/




Krishnan Mahadevan

unread,
Jul 4, 2012, 3:57:25 AM7/4/12
to seleniu...@googlegroups.com
What error are you getting on the eclipse IDE ?

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"







--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en-US.

Rajiv Nanduani

unread,
Jul 4, 2012, 4:02:13 AM7/4/12
to seleniu...@googlegroups.com
first let me know which one is the correct syntex

i am getting for

driver.manage().addCookie(new Cookie("foo", "bar", "www.google.com", "/", null));

The method addCookie(Cookie) in the type WebDriver.Options is not applicable for the arguments (Cookie)



for driver.manage().addCookie( 'path=/; domain=.example.com');

Invalid character constant
--

Krishnan Mahadevan

unread,
Jul 4, 2012, 4:28:43 AM7/4/12
to seleniu...@googlegroups.com
This should help you figure out things



Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



Rajiv Nanduani

unread,
Jul 4, 2012, 10:58:54 AM7/4/12
to seleniu...@googlegroups.com
no its not working
Please anyone have used add cookies method using web driver.
please send me the example to set the below



NAME betaaccess
VALUE true
DOMAIN abc.com
PATH /
EXPIRES 7/6/2012 10:26:47 AM


Tom

unread,
Jul 4, 2012, 11:36:02 AM7/4/12
to seleniu...@googlegroups.com
I'm using:

Cookie cookie = new Cookie.Builder("name", "value").domain("domain").build()
driver.manage().addCookie(cookie)


Am Mittwoch, 4. Juli 2012 16:58:54 UTC+2 schrieb rajivkumarnandvani:
no its not working
Please anyone have used add cookies method using web driver.
please send me the example to set the below


NAME betaaccess
VALUE true
DOMAIN abc.com
PATH /
EXPIRES 7/6/2012 10:26:47 AM


To post to this group, send email to selenium-users@googlegroups.com.
To unsubscribe from this group, send email to selenium-users+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en-US.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to selenium-users@googlegroups.com.
To unsubscribe from this group, send email to selenium-users+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en-US.



--

Regards

RAJIV KUMAR NANDVANI

http://rajivkumarnandvani.wordpress.com
http://testeverythingqtp.blogspot.com/




--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to selenium-users@googlegroups.com.
To unsubscribe from this group, send email to selenium-users+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en-US.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to selenium-users@googlegroups.com.
To unsubscribe from this group, send email to selenium-users+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en-US.

Darrell Grainger

unread,
Jul 4, 2012, 2:05:58 PM7/4/12
to seleniu...@googlegroups.com
First, you can only add cookies to the current domain. If I am on www.google.com and I try adding a cookie to the domain www.yahoo.com it will fail. So, assuming you are access the domain abc.com and want to add the cookie listed below, the code would be:

Calendar c = new GregorianCalendar();
int year = 2012;
int month = 6;
int day = 7;
int hour = 10;
int minute = 26;
int second = 47;
c.set(year, month, day, hour, minute, second);
Date expiry = c.getTime();
String name = "betaaccess";
String value = "true";
String domain = "www.abc.com";
String path = "/";
Cookie cookie = new Cookie(name, value, domain, path, expiry);
driver.manage().addCookie(cookie);



Darrell

Rajiv Nanduani

unread,
Jul 5, 2012, 12:16:57 AM7/5/12
to seleniu...@googlegroups.com
I am getting this error



The method addCookie(Cookie) in the type WebDriver.Options is not applicable for the arguments (Cookie)

i am using webdribver selenium java 2.21.0 & server stand alone 2.21.0

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/k3TrXYBWhXUJ.

To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en-US.



--

Darrell Grainger

unread,
Jul 5, 2012, 12:35:02 PM7/5/12
to seleniu...@googlegroups.com
I suspect that you are passing in the wrong type of Cookie class. There are a number of different Cookie classes available in Java. The addCookie method takes in a org.openqa.selenium.Cookie. I have attached a simple program which should work. It has all the code in it. You should be able to compile it and run it. The only thing you need to do is set up a project with the Selenium jar files. In Eclipse I did the folowing:

  1. Create a Java Project called Test
  2. Right click on the Test project, Build Path->Configure Build Path...
  3. Go to the Libraries tab
  4. Select Add Externals JARs...
  5. Select the jar files which are required for Selenium client
  6. Import the attached Test.java to the project
  7. Run it
If you get build errors, you probably didn't include the right JAR files. If you don't have Firefox installed, you'll need to change the WebDriver to InternetExplorerDriver or some browser which you do have installed.

Darrell

To post to this group, send email to selenium-users@googlegroups.com.
To unsubscribe from this group, send email to selenium-users+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en-US.
Test.java

Rajiv Nanduani

unread,
Jul 9, 2012, 4:49:58 AM7/9/12
to seleniu...@googlegroups.com, darrell....@gmail.com
Darrell Grainger, Thanks a lot. My mistake. Ya its my fault I did not check it was using wrong cookie class. Now its working fine.


To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/xdb2wyq207cJ.

To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en-US.

vineet saini

unread,
Jan 28, 2014, 4:33:22 PM1/28/14
to seleniu...@googlegroups.com

Is there solution to this problem?  I get error when setting cookie:
You may only add cookies that would be visible to the current domain

Steps:
WebDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_17);
Cookie C1 = new Cookie("Test1", "10",".mytest.com","/",null,false);

//driver.get("http://mytest.com.com/sometest");

try{
     driver.manage().addCookie(C1);
  }catch(Exception e){ System.out.println(e.getMessage()); }

driver.get("http://www.mytest.com/testservice?testid=1");


But If I do make get call, set cookie and do get in that case it let me add cookie. Which is over kill for my test as it require to make two calls.
In addition to that if initial get return non HTML contents it give me another odd error as below:
You may not set cookies on a page that is not HTML

Karim NABLI

unread,
Apr 1, 2014, 2:30:56 PM4/1/14
to seleniu...@googlegroups.com
Hi to all,
In addition to that,  it is impossible to set cookie domain different to the target url domain which is not pleasant at all as sometimes I need to tests vs localhost or using virtual ip address an target the right domain.
regards,
Reply all
Reply to author
Forward
0 new messages