c# - Web API启动流程

标签 c# selenium phantomjs

我相信我从 Web API 服务运行 Selenium PhantomJS 驱动程序时遇到权限问题。我尝试通过各种管理员帐户模拟该服务,但仍然遇到相同的错误。如何确定哪个配置文件引发错误?我通过打印 process.StandardOutput.ReadToEnd() 收到此错误

System.Configuration.ConfigurationErrorsException: Configuration system failed to  initialize
  ---> System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: Access is denied.
  ---> System.Security.SecurityException: Access is denied.
   at System.Security.Principal.WindowsIdentity.SafeImpersonate(SafeTokenHandle userToken, WindowsIdentity wi, StackCrawlMark& stackMark)
   at System.Security.Principal.WindowsIdentity.SafeRevertToSelf(StackCrawlMark& stackMark)
   at System.Security.Principal.WindowsIdentity.Impersonate(IntPtr userToken)
   at System.Configuration.ClientConfigurationHost.Impersonate()

最佳答案

这很奇怪,因为 Web 驱动程序不需要您动态更改配置文件。我想你正在尝试自己做所有事情:不要重新发明轮子,因为所有这些东西都已经由 Selenium 支持类完成了。

为了在 Web Api 中试用 WebDriver:

获取 https://stackoverflow.com/ 标题的非常基本的方法可能是:

using OpenQA.Selenium;
using OpenQA.Selenium.PhantomJS;
...
public string Get()
{
    // c:\phantomjs contains phantomjs.exe
    // if blank, Web Driver will download the latest version
    IWebDriver driver = new PhantomJSDriver(@"c:\phantomjs");
    driver.Navigate().GoToUrl("https://stackoverflow.com/");
    string title = driver.Title;
    driver.Quit();
    return title;
}
  • 就这些了!

根据您的主机,支持类在尝试自行启动 phantomjs 时可能会出现一些问题;只需将应用程序池身份更改为高级用户即可。

关于c# - Web API启动流程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18748636/

相关文章:

windows - 为 cmd 启用颜色输出

javascript - Phantomjs 替代方案 - 需要 RFC6455 WebSocket 标准

c# - 如何引用和上传Azure Blob存储中的文件?

java - 如何将断言添加到 if ..else 条件

c# - ListView 中的文本框

java - 单击列表底部获取列表顶部的元素

java - 如果它不能为空,但需要初始化,我该怎么办?

javascript - PhantomJS 主机文件更改或主机 header

c# - 为什么 foreach 循环在 gridview 的最后一行失败?

c# - 在网格布局中创建动态按钮 - 创建幻方 UI