48

Following is my code :

case BrowserType.PhantomJS:
               var service = PhantomJSDriverService.CreateDefaultService(Path.Combine(_rootPath, @"Packages\"));
               var cookieFilePath=Path.Combine(_rootPath, @"Packages\cookie.txt");
                 if (!File.Exists(cookieFilePath))
                       File.Create(cookieFilePath);

                 var phantomjsoptions = new PhantomJSOptions();
                 driver = new PhantomJSDriver(service,phantomjsoptions);
                 var cookieJar = driver.Manage().Cookies;
                 driver.Navigate().GoToUrl(SeleniumConfiguration.Current.BaseURL);
                 cookieJar.AddCookie(new Cookie("x", "12345"));
                 return driver;

Basically the issue is that i am not able to login into my test application because i get an error saying -

"Your browser is set to block cookies"

I've tried everything but i just can't seem to get the solution for this.
what should i do?
Please help me out here.
Let me know if there is some detail missing.

iamrajshah
  • 963
  • 1
  • 11
  • 23
Prateek
  • 627
  • 1
  • 7
  • 19
  • 9
    Cookies are enabled by default. There is probably another issue. – Artjom B. Aug 12 '15 at 16:46
  • 1
    This has almost certainly nothing to do with any cookie settings in PhantomJS. It's either an SSL problem or a JavaScript problem. What's the site you're trying to visit and what's your PhantomJS version? – Artjom B. Aug 15 '15 at 10:29
  • Its the latest version i suppose. I'm not sure if i can share the site name here... – Prateek Aug 15 '15 at 10:59
  • @Prateek can you add the full stack trace ? – sm_ Aug 21 '15 at 10:41
  • did you try the same with another selenium driver, .i.e. Firefox and/or Chrome? What was the result there? – luksch Aug 21 '15 at 11:15
  • @luksch It works fine for other drivers. This problem is only specific to Phantonjs driver. Siraj Mansour, there's not really any stack trace, because the only exception occuring is in the automation script, which ends up failing because the login fails, coz of the issue i stated in my problem. – Prateek Aug 21 '15 at 18:15
  • Have a look at this SO post. http://stackoverflow.com/questions/30072314/enable-cookies-in-phantomjs-selenium – Stanimir Dimitrov Aug 22 '15 at 04:03
  • I doubt its a cookie problem. Set your service to ignore ssl errors and remove the cookie management code. – SILENT Nov 05 '15 at 02:54
  • RFC 2109 explicitly forbids cookie accepting from URLs with IP address. Are you accessing the test page using a IP based URL? – Aron Feb 02 '19 at 10:59

3 Answers3

1

RFC 2109 explicitly forbids cookie accepting from URLs with IP address

You are almost certainly accessing your test server via an IP based address.

You can try set up some kind of DNS/host file to allow you to use a fake domain name.

Aron
  • 15,464
  • 3
  • 31
  • 64
  • Wow, i'm in such a luck, I have been struggeling with a `non-related` problem for a couple of weeks now. Our testservers' adress was IP based, and we thought it was some error in the configuration, we've literally spent days on this. I just fired up my `hosts` file and added a DNS to the IP. worked like a charm. Thank you! – Joel Feb 05 '19 at 14:29
0

You must wait while page was loaded and then set cookie:

driver.Navigate().GoToUrl(SeleniumConfiguration.Current.BaseURL);
//Wait page loaded
cookieJar.AddCookie(new Cookie("x", "12345"));

Try this solution: https://stackoverflow.com/a/30636987

driver.Navigate().GoToUrl(SeleniumConfiguration.Current.BaseURL);//some fake url
driver.Manage().Window.Maximize();
driver.SwitchTo().ActiveElement();
cookieJar.AddCookie(new Cookie("x", "12345"));
driver.Navigate().GoToUrl(SeleniumConfiguration.Current.BaseURL);//cookie exsist
Community
  • 1
  • 1
Dmitrii Zyrianov
  • 2,208
  • 1
  • 21
  • 27
-2

You can try below steps

1) Create a user profile on firefox or in chrome browser. 2) Confirm that "accept cookies" option is enabled by going to browsers settings option. 3) Load your profile via selenium.

Doing so it will ensure that enable cookie option is ON and your sessions would also get saved in the browser cache.

for chrome

System.setProperty("webdriver.chrome.driver", "browser/chromedriverlinux");
ChromeOptions options = new ChromeOptions();
options.addArguments("--user-data-dir=/home/rohit/.config/google-chrome/Profile 1");
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);

For Firefox

ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffprofile = profile.getProfile("ROHIT");
WebDriver driver = new FirefoxDriver(ffprofile);

To create profile in firefox try below command in terminal firefox -p

Rohit Naik
  • 19
  • 4