Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PhantomJSC#中启用cookie?_C#_Selenium_Automation_Phantomjs_Headless - Fatal编程技术网

如何在PhantomJSC#中启用cookie?

如何在PhantomJSC#中启用cookie?,c#,selenium,automation,phantomjs,headless,C#,Selenium,Automation,Phantomjs,Headless,以下是我的代码: case BrowserType.PhantomJS: var service = PhantomJSDriverService.CreateDefaultService(Path.Combine(_rootPath, @"Packages\")); var cookieFilePath=Path.Combine(_rootPath, @"Packages\cookie.txt");

以下是我的代码:

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;
基本上,问题是我无法登录到我的测试应用程序,因为我得到一个错误,说-

“您的浏览器已设置为阻止Cookie”

我什么都试过了,但似乎找不到解决办法。
我该怎么办?
请帮帮我。

如果缺少某些详细信息,请告诉我。

加载页面时,您必须等待,然后设置cookie:

driver.Navigate().GoToUrl(SeleniumConfiguration.Current.BaseURL);
//Wait page loaded
cookieJar.AddCookie(new Cookie("x", "12345"));
尝试以下解决方案:


您可以尝试以下步骤

1) 在firefox或chrome浏览器中创建用户配置文件。 2) 通过转到浏览器设置选项,确认“接受cookies”选项已启用。 3) 通过selenium加载您的个人资料

这样做将确保启用cookie选项,并且您的会话也将保存在浏览器缓存中

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);
对于Firefox

ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffprofile = profile.getProfile("ROHIT");
WebDriver driver = new FirefoxDriver(ffprofile);
要在firefox中创建配置文件,请在终端中尝试以下命令
firefox-p

RFC2109明确禁止从具有IP地址的URL接受cookie

您几乎肯定是通过基于IP的地址访问测试服务器


您可以尝试设置某种DNS/主机文件,以允许您使用假域名。

默认情况下启用Cookie。可能还有另一个问题。这几乎肯定与PhantomJS中的任何cookie设置无关。这要么是SSL问题,要么是JavaScript问题。你想访问的网站是什么?你的PhantomJS版本是什么?我想是最新版本吧。我不确定我是否可以在这里共享站点名称…@Prateek你可以添加完整的堆栈跟踪吗?你是否尝试过其他selenium驱动程序,例如。Firefox和/或Chrome?结果是什么?PhantomJSDriver,不是Chromew,我很幸运,我已经和一个不相关的问题纠缠了好几个星期了。我们的测试服务器的地址是基于IP的,我们认为这是配置中的一些错误,实际上我们已经为此花费了数天时间。我刚刚启动了我的
hosts
文件,并向IP添加了一个DNS。工作得很有魅力。非常感谢。
ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffprofile = profile.getProfile("ROHIT");
WebDriver driver = new FirefoxDriver(ffprofile);