首页 > 解决方案 > NET Core 上的 Selenium 在 Linux 上的工作方式不同

问题描述

我使用 net core 和 selenium 开发了一个小爬虫,但我注意到我的程序在 windows 上可以正常工作,而在 centos 上却不能。似乎centos上的驱动程序对象具有与windows完全不同的结构。

这是我的测试代码:

    class Program
    {
        static async Task Main(string[] args)
        {
            IWebDriver driver = SetupTest2();
            driver.Navigate().GoToUrl("https://google.com");
            await Task.Delay(5000);
            Console.WriteLine(driver.PageSource);
        }

        public static IWebDriver SetupTest2()
        {
            String driverPath = @"/root";
            String driverExecutableFileName = "chromedriver";
            ChromeOptions options = new ChromeOptions();
            options.AddArgument("--headless");
            options.AddArgument("--no-sandbox");
            ChromeDriverService service = ChromeDriverService.CreateDefaultService(driverPath, driverExecutableFileName);
            IWebDriver driver = new ChromeDriver(service, options, TimeSpan.FromSeconds(30));
            return driver;
        }        
    }

在 Windows 上它可以正常工作。Console.WriteLine 打印页面的源代码。而是在centos上打印

System.Collections.Generic.Dictionary`2[System.String,System.Object]

我能做些什么来解决这个问题吗?或者我必须切换到与 python 不同的东西?

标签: c#seleniumdictionary

解决方案


推荐阅读