问题:
最近我的代码在别人的机器上运行,打不开浏览器, 在调用IWebDriver driver = new ChromeDriver() 时候报错:
Message: Unable to create instance of class. Error: System.TypeInitializationException: The type initializer for 'XXXXX' threw an exception. ---> OpenQA.Selenium.WebDriverException: unknown error: cannot find Chrome binary
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 10.0.14393 x86_64).
原因:
问题的原因看起来是Selenium 默认会到C:\Program Files (x86)\Google\Application的目录下去找chorme.exe的文件, 以前我的电脑安装的是chrome 32位,会安装在C:\Program Files (x86)的目录下, 而别人的电脑安装的是chrome 64位, 会安装在C:\Program Files目录下,所以去X86目录下去找chorme.exe的文件的文件就会找不到报错
解决方法:
chromeOptions.BinaryLocation = "C:/Program Files/Google/Chrome/Application/chrome.exe";
chromeOptions.AddUserProfilePreference("download.default_directory", DownDiretoryPath);
driver = new ChromeDriver(executableLocation, chromeOptions, TimeSpan.FromSeconds(120));