C# 使用Chromedriver报错:无匹配SERVICE、OPTIONS、PATH三参数的构造函数
解决方案
你提到的三个参数不需要独立的新重载即可实现,Selenium的ChromeDriver设计逻辑里,驱动路径本身就是ChromeDriverService的配置项,直接通过现有重载即可满足需求:
方案1:直接使用现有重载(推荐)
在创建ChromeDriverService的时候就传入你的chromedriver目录路径,之后直接调用双参数的构造函数传入service和options即可,相当于同时配置了路径、service自定义规则、浏览器参数三个维度的内容:
// 第一步:传入驱动路径创建service实例,可自行配置service的其他属性 var driverService = ChromeDriverService.CreateDefaultService(@"C:\你的\chromedriver\存放目录"); // 示例:自定义service的端口、日志路径 driverService.Port = 9515; driverService.EnableVerboseLogging = true; driverService.LogPath = "chromedriver_run.log"; // 第二步:配置Chrome启动参数 var chromeOptions = new ChromeOptions(); chromeOptions.AddArgument("--headless=new"); chromeOptions.AddArgument("--disable-dev-shm-usage"); // 第三步:调用现有构造函数,三个需求全部覆盖 using var driver = new ChromeDriver(driverService, chromeOptions);
方案2:封装自定义构造函数
如果你确实需要你描述的三参数形式的构造函数,可以继承ChromeDriver实现自定义子类:
public class ExtendChromeDriver : ChromeDriver { // 实现你需要的三参数构造函数 public ExtendChromeDriver(string chromeDriverDirectory, ChromeDriverService service, ChromeOptions options) : base(UpdateServicePath(service, chromeDriverDirectory), options) { } private static ChromeDriverService UpdateServicePath(ChromeDriverService service, string driverPath) { service.DriverServicePath = driverPath; return service; } } // 调用示例和你期望的完全一致 using var driver = new ExtendChromeDriver(@"C:\驱动路径", yourCustomService, yourOptions);
内容的提问来源于stack exchange,提问作者Martin Ma
相关产品推荐
相关产品推荐

