如何在 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