c# - 如何使用 ChromeDriver 版本 83.0.4103.39 和 Selenium 使用 user-data-dir 标志启动 Chrome 无头
问题描述
代码
ChromeOptions options = new ChromeOptions();
options.AddArguments("--headless");
options.AddArguments("--disable-gpu");
options.AddArguments("--disable-software-rasterizer");
options.AddArguments("--user-data-dir=/profiles/" + profile);
options.AddArguments("--disable-software-rasterizer");
options.AddArguments("--window-size=1920x1080");
options.AddArguments("--disable-extensions");
options.AddArguments("--disable-plugins-discovery");
IWebDriver webDriver = new ChromeDriver(options);
WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(15));
webDriver.Navigate().GoToUrl(@"someUrl"); ---> Here code stucks
错误:
OpenQA.Selenium.WebDriverException:'对 URL http://localhost:64225/session 的远程 WebDriver 服务器的 HTTP 请求在 60 秒后超时。
我也试过
options.AddArguments("--disable-dev-shm-usage");
options.AddArguments("--no-sandbox");
控制台输出
"A cookie associated with a cross-site resource at
<SomeSite> was set without the `SameSite` attribute.
A future release of Chrome will only deliver cookies
with cross-site requests if they are set with
`SameSite=None` and `Secure`. You can review cookies
in developer tools under Application>Storage>Cookies
and see more details at
https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.",
source: <someURL> (0)
解决方案
此错误消息...
OpenQA.Selenium.WebDriverException: 'The HTTP request to the remote WebDriver server for URL http://localhost:64225/session timed out after 60 seconds.
...意味着ChromeDriver无法使用所需的Chrome Profile启动/生成新的浏览上下文,即Chrome 浏览器会话。
根据如何通过 Selenium 的 --user-data-dir 参数打开 Chrome 配置文件中的讨论,而不是通过 仅指定目录名称user-data-dir
,您需要传递.user-data-dir
解决方案
所以你需要替换这行代码:
options.add_argument("user-data-dir=bot_data")
和:
options.add_argument("user-data-dir=C:\\Users\\AtechM_03\\AppData\\Local\\Google\\Chrome\\User Data\\bot_data")
参考
您可以在以下位置找到一些相关的讨论:
奥特罗
一些相关文件:
推荐阅读
- excel - 如何在 vba 中从我的用户窗体调用模块?
- python - 如何在 Python 中开始这个类型转换作业?
- macos - 为什么 Rust 会发出 280 行汇编代码来打印“Hello, world”?
- jquery - Bootstrap 弹出框需要单击两次才能打开弹出框
- android - 更改软键盘高度
- java - 无法使用 Scanner Java 读取斜杠 ( / )
- ios - 从 Pushwoosh 切换后 OneSignal 订阅者未更新
- api - salesforcerest.update 在 WSO2 EI 6.1.1 中不起作用
- c++ - C++ 这个函数调用之前的 (void) 有什么作用?
- python - Converting PDF document to DataFrame