代码
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:'到远程的HTTP请求URL的WebDriver服务器http://localhost:64225/session超时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配置文件启动/生成新的浏览上下文,即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")
参考
你可以在中找到一些相关的讨论
- 如何在Selenium Webdriver Python 3中使用Chrome配置文件
- Selenium:指向默认Chrome会话
Outro
一些相关文件:
- 无头Chrome中的会话隔离
- headless:引入浏览器上下文
- 保存和恢复浏览器会话
- Headless维护与headful不同的配置文件文件夹结构