我开始研究Selenium (C#),令我惊讶的是,我甚至无法从硒文档上运行最简单的项目。然而,我的同事们可以正常地运行它,没有问题。
我唯一能够运行的方法是将--no-sandbox
选项添加到Chrome选项中。Firefox (Gekco)驱动程序没有这个问题,我的Chrome版本和我的Chromedriver版本匹配。但我不能对每个项目都这样做,因为我们使用的是一个不通过这个选项的库,它超出了我的控制范围。
我可能需要更改的策略/配置是什么?
我试图在S/O上找到其他类似的线程,但这些都解决不了我的问题。大多数问题都可以通过更新Chrome/ChromeDriver版本来解决,但我的最迟也是100版本( Chrome 99也有同样的问题)。
错误
它只是停留在data:,
上,并不像它应该的那样继续到任何其他页面。
Starting ChromeDriver 100.0.4896.60 (6a5d10861ce8de5fce22564658033b43cb7de047-refs/branch-heads/4896@{#875}) on port 61538
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
DevTools listening on ws://127.0.0.1:61542/devtools/browser/38c3268c-dd43-4100-bb1a-71aa2af4c1fc
Unhandled exception. OpenQA.Selenium.WebDriverException: The HTTP request to the remote WebDriver server for URL http://localhost:61538/session timed out after 60 seconds.
---> System.Threading.Tasks.TaskCanceledException: The request was canceled due to the configured HttpClient.Timeout of 60 seconds elapsing.
---> System.TimeoutException: The operation was canceled.
---> System.Threading.Tasks.TaskCanceledException: The operation was canceled.
---> System.IO.IOException: Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request..
---> System.Net.Sockets.SocketException (995): The I/O operation has been aborted because of either a thread exit or an application request.
--- End of inner exception stack trace ---
代码
using System;
using System.Threading;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
namespace SeleniumDocs.Hello
{
public class HelloSelenium
{
public static void Main()
{
var chromeOptions = new ChromeOptions();
var service = ChromeDriverService.CreateDefaultService(@"C:\WebDriver\bin");
// chromeOptions.AddArgument("--no-sandbox"); // with this it works; without, doesn't
var driver = new ChromeDriver(service, chromeOptions);
driver.Navigate().GoToUrl("https://selenium.dev");
Thread.Sleep(10000);
driver.Quit();
}
}
}
发布于 2022-03-31 22:45:35
沙盒
沙盒是一个C++库,它允许创建沙箱化进程--在非常严格的环境中执行的进程。沙箱化进程可以自由使用的唯一资源是CPU周期和内存。例如,沙箱进程不能写入磁盘或显示自己的窗口。他们究竟能做什么,是由一项明确的政策控制的。铬渲染机是砂箱工艺。
从功能上讲,沙箱限制了在沙箱内运行的代码中错误的严重性。这些bug无法在用户帐户中安装持久恶意软件(因为禁止写入文件系统)。这样的错误也不能从用户的机器上读取和窃取任意文件。
(在铬中,渲染程序是沙箱的,并且有这种保护。在NPAPI删除之后,所有剩下的插件也会被沙箱化。铬呈现程序过程与系统分离,但尚未与网络分离。因此,还没有提供基于域的数据隔离.
这个酶
Chrome在启动时崩溃的一个常见原因是在Linux上以root
用户(administrator
)的身份运行Chrome。虽然可以通过在创建--no-sandbox
会话时传递WebDriver标志来解决此问题,但这种配置是不受支持的,并且非常不受欢迎。您需要将您的环境配置为作为常规用户运行Chrome。
参考文献
您可以在以下网站找到相关的详细讨论:
https://stackoverflow.com/questions/71695438
复制相似问题