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

How to set a relative path in C# selenium for IE driver

在下面的代码中,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