繁体   English   中英

如何在phantomjsdriver selenium c#中启用cookie?

[英]How to enable cookie in phantomjsdriver selenium c#?

以下是我的代码:

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”

我已经尝试了一切,但我似乎无法得到解决方案。
我该怎么办?
请帮帮我。
如果遗漏了一些细节,请告诉我。

RFC 2109明确禁止cookie接受来自具有IP地址的URL

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

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

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

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

试试这个解决方案: 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

您可以尝试以下步骤

1)在firefox或chrome浏览器中创建用户配置文件。 2)通过转到浏览器设置选项确认启用了“接受cookie”选项。 3)通过硒加载您的配置文件。

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

用于镀铬

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中尝试以下命令

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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