微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 C# selenium 中为 IE 驱动程序设置相对路径

如何解决如何在 C# selenium 中为 IE 驱动程序设置相对路径

在下面的代码中,IEDriverServer.exe 在我的本地机器上可用。我想在不同的机器上运行这段代码。 C#获取IEDriverServer.exe时如何设置相对路径

InternetExplorerOptions options = new InternetExplorerOptions()
            {
                ForceCreateProcessApi = true,browserCommandLineArguments = "-private",IntroduceInstabilityByIgnoringProtectedModeSettings = true,IgnoreZoomLevel = true
                
            };
            IWebDriver driver = new InternetExplorerDriver(Path.GetFullPath("C:\\Users\\kamal\\Documents\\Sample\\Sample\\bin\\Debug"),options);

            driver.Navigate().GoToUrl("https://www.google.com");

            driver.Quit();

解决方法

如果文件的路径是 C:\Users\kamal\Documents\Sample\Sample\bin\Debug\IEDriverServer.exe

相对路径是:

driver = new InternetExplorerDriver(Path.GetFullPath(@"..\"),options); 

如果是 C:\Users\kamal\Documents\Sample\Sample\bin\Debug\netcoreapp3.1\IEDriverServer.exe

你可以使用driver = new InternetExplorerDriver(".",options);

更详细的路径是这样的:

var projectRoot = Path.GetFullPath(@"..\..\..\"); //which in your case is C:\\Users\\kamal\\Documents\\Sample\\Sample\\
var projectRootBin = Path.GetFullPath(@"..\..\"); // C:\\Users\\kamal\\Documents\\Sample\\Sample\\bin
var projectRootBinDebug = Path.GetFullPath(@"..\"); // C:\\Users\\kamal\\Documents\\Sample\\Sample\\bin\Debug
var projectRootBinDebugNetCoreApp31 = Path.GetFullPath("."); // C:\\Users\\kamal\\Documents\\Sample\\Sample\\bin\Debug\\netcoreapp3.1

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。