Selenium + C# - 如何在具有 chrome.options 的同时设置 chrome 驱动程序路径

Selenium + C# - How to set chrome driver path whilst having chrome.options

如何在 chrome.options 的情况下设置 chrome 驱动程序路径。目前我正在创建选项以删除 chrome 中的 mic/camera 权限,但我必须将选项路径提供给驱动程序。

这是我目前的一个想法代码。 chrome 驱动程序正在启动并在应用 chrome.options 设置的情况下正常工作,但我想知道一种在创建新驱动程序时提供变量 chromeDriverPath 和选项的方法

我是 selenium 的新手,所以请不要评判 :)

        IWebDriver driver;
    string chromeDriverPath;        

    void SetChromeDriverPath()
    {
        OperatingSystem os = Environment.OSVersion;
        string osName = os.Platform.ToString().ToLower();
        if (osName == "win32nt")
            chromeDriverPath = Environment.CurrentDirectory.ToString() + "\tools\selenium\chrome\windows\";
        if (osName == "unix")
            chromeDriverPath = Environment.CurrentDirectory.ToString() + "\tools\selenium\chrome\linux\";
        if (osName == "macosx")
            chromeDriverPath = Environment.CurrentDirectory.ToString() + "\tools\selenium\chrome\mac\";
    }
            
    public IWebDriver GetChromeDriver()
    {
        ChromeOptions options = new ChromeOptions();
        options.AddArgument("use-fake-ui-for-media-stream");
        options.AddArgument("--start-maximized");
        SetChromeDriverPath();
        driver = new ChromeDriver(options);
        return driver;
    }

https://www.selenium.dev/selenium/docs/api/dotnet/html/T_OpenQA_Selenium_Chrome_ChromeDriver.htm

您可以看到 chromedriver 路径应该作为 chromedriver 的参数,因此您可以使用:

driver = new ChromeDriver(chromeDriverPath ,options);