我相信我从 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:
- 创建/添加 Web Api 项目
- 添加对 Nuget 包的引用 Selenium WebDriver Support Classes
- 将网络驱动程序代码添加到您的操作
获取 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/