我的谷歌浏览器版本:88.0.4324.150
我找到了一些解决方案,但它们对我不起作用。 我想打开 Google Chrome 并抛出 Selenium,但它还不起作用。
错误
System.InvalidOperationException
HResult=0x80131509
Message=session not created: This version of ChromeDriver only supports Chrome version 85 (SessionNotCreated)
Source=WebDriver
StackTrace:
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeOptions options)
at RSelenium.start_Browser() in C:\Users\ruper\source\repos\T\T\RSelenium.cs:line 30
at T.Form1.btn_selenium_Click(Object sender, EventArgs e) in C:\Users\ruper\source\repos\T\T\Form1.cs:line 61
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at T.Program.Main() in C:\Users\ruper\source\repos\T\T\Program.cs:line 47
代码
System.Environment.SetEnvironmentVariable("webdriver.chrome.driver", @"C:\Program Files\Google\Chrome\Application\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.AddArguments("start-maximized");
options.AddArguments("disable-infobars");
options.AddArguments("--disable-extensions");
options.AddArguments("--disable-gpu");
options.AddArguments("--disable-dev-shm-usage");
options.AddArguments("--no-sandbox");
IWebDriver driver = new ChromeDriver(options);
从这里下载 chrome 驱动程序 88
https://www.selenium.dev/selenium/docs/api/dotnet/html/T_OpenQA_Selenium_Chrome_ChromeDriver.htm
你可以看到构造函数接受可执行文件的路径
所以使用
IWebDriver driver = new ChromeDriver("c:/DownloadeddirectoryPath",options);
注意:路径应该是包含 chromedriver.exe 的目录的 PATH,而不是 chromedriver.exe 本身
确保 webdriver 和 Chrome(实际上我建议 Chromium)版本相同的另一种方法是从同一个 Chromium 快照页面下载两者。 https://commondatastorage.googleapis.com/chromium-browser-snapshots/index.html
例如,v96 Chromium Linux 和相应的 Web 驱动程序位于此处: https://commondatastorage.googleapis.com/chromium-browser-snapshots/index.html?prefix=Linux_x64/929513/
稳定版本的内部版本号可以在这里找到:https://github.com/Bugazelle/chromium-all-old-stable-versions
PDHide 已经描述了如何指向 webdriver 路径。要指向 Chromium 二进制文件/可执行文件,请使用
var options = new ChromeOptions();
options.BinaryLocation = "/full/location/including/binary";
var driver = new ChromeDriver("/folder/where/webdriver/located", options);
@PDHide 的解决方案解决了我的问题,但我又遇到了同样的问题。 我现在已经这样解决了:
我已将 Nuget 中的 WebDriverManager 添加到我的项目中。 我已将这一行添加到 onload:
new DriverManager().SetUpDriver(new ChromeConfig(), VersionResolveStrategy.MatchingBrowser);
我创建了一个返回 WebDriver 对象的方法:
public IWebDriver GetChromeDriver()
{
ChromeOptions options = new ChromeOptions();
options.AddArgument("no-sandbox");
string[] files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + "/Chrome", "ChromeDriver.exe", SearchOption.AllDirectories);
List<KeyValuePair<string, DateTime>> lstFile = new List<KeyValuePair<string, DateTime>>();
foreach (string file in files)
{
lstFile.Add(new KeyValuePair<string, DateTime>(file, File.GetLastWriteTime(file)));
}
string filePath = System.IO.Path.GetDirectoryName(lstFile.OrderByDescending(r => r.Value).First().Key);
return new ChromeDriver(filePath, options);
}
我这样称呼它:
IWebDriver myDriverChrome = new BasicOperations().GetChromeDriver();