`
银辰宇
  • 浏览: 4350 次
文章分类
社区版块
存档分类
最新评论

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

阅读更多

  有朋友给我这样一段代码:

  

  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”));

  

  基本问题是无法登录测试应用程序,因为得到一个错误提示 “你的浏览器设置为阻止cookie”,尝试过很多办法,但无法得到解决方案 ....

  

  实际上以下步骤即可解决:

  

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

  

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

  

  对于chrome

  

  System.setProperty(“webdriver.chrome.driver”,“browser / chromedriverlinux”);

  

  ChromeOptions选项=新的ChromeOptions();

  

  options.addArguments(“ -  user-data-dir = / home / rohit / .config / google-chrome / Profile 1”);

  

  options.addArguments( “ - 启动最大化”);

  

  WebDriver驱动程序=新的ChromeDriver(选项);

  

  对于Firefox

  

  ProfilesIni profile = new ProfilesIni();

  

  FirefoxProfile ffprofile = profile.getProfile(“ROHIT”);

  

  WebDriver driver = new FirefoxDriver(ffprofile);

 

       文章部分资料来源:https://jlcaqx.weebly.com/ 如需转载请注明

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics