在 Selenium C# 中初始化 Web 驱动程序时如何解决无效参数错误?

问题描述 投票:0回答:1

我下载了用于抓取的 chrome Web 驱动程序,然后安装了 C# Nuget Selenium 包并在下面编写了以下代码来启动抓取过程,然后我收到一条异常消息,指出我在提供的选项中使用了无效参数Web 驱动程序构造函数

 try
     {
     string chrome_path = "C:\\Users\\HP\\PycharmProjects\\Scraping\\chromedriver.exe";
     //try and use the web driver to mine for the odds from the site
     var data = new Dictionary<string, List<string>>()
     {
         {"Home", new List<string>() },
         {"Draw", new List<string>() },
         {"Away", new List<string>() },
     };
     //define options to run the driver
     var options = new ChromeOptions();
     options.AddArgument("--headless");
     options.AddArgument("--no-sandbox");
     options.AddArgument("--disable-dev-shm-usage");
     //create a new driver
     using (var driver = new ChromeDriver(chrome_path, options))
     {
         //navigate to the required URL
         driver.Navigate().GoToUrl("https://www.betexplorer.com");
         //scroll the page for the elements to become fully rendered

     }
 }catch (WebDriverArgumentException ex)
     {
         MessageBox.Show($"An error occurred: {ex.Message}");
     }
 }

错误

invalid argument session-info: chrome-headless-shell=125.0.6422.113

c# selenium-webdriver
1个回答
0
投票

从 Selenium 4.6 开始,添加了 SeleniumManager,它会自动为您下载和配置适当的驱动程序。因此,您不再需要使用 DriverManager 或指定路径等。

您的代码可以简化为

try
{
    //try and use the web driver to mine for the odds from the site
    var data = new Dictionary<string, List<string>>()
    {
        {"Home", new List<string>() },
        {"Draw", new List<string>() },
        {"Away", new List<string>() },
    };
    //define options to run the driver
    var options = new ChromeOptions();
    options.AddArgument("--headless");
    options.AddArgument("--no-sandbox");
    options.AddArgument("--disable-dev-shm-usage");
    //create a new driver
    using (var driver = new ChromeDriver(options))
    {
        //navigate to the required URL
        driver.Navigate().GoToUrl("https://www.betexplorer.com");
        //scroll the page for the elements to become fully rendered

    }
}
catch (WebDriverArgumentException ex)
{
    MessageBox.Show($"An error occurred: {ex.Message}");
}
© www.soinside.com 2019 - 2024. All rights reserved.