使用此页面作为参考: http://executeautomation.com/blog/running-chrome-in-headless-mode-with-selenium-c/
我试图让Chrome在无头模式下运行(以便在SpecFlow / xUnit下运行)。这已经失败了,我想知道是否有人有解决方案/修复。
我在Windows 7上使用.NET 4.7控制台应用程序和Google Chrome版本65.0.3325.162(官方版本)(64位)运行VS 2015 ...
这是我的packages.config:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Selenium.Support" version="3.11.0" targetFramework="net461" />
<package id="Selenium.WebDriver" version="3.11.0" targetFramework="net461" />
<package id="Selenium.WebDriver.ChromeDriver" version="2.36.0" targetFramework="net461" />
<package id="xunit" version="2.3.1" targetFramework="net461" />
<package id="xunit.abstractions" version="2.0.1" targetFramework="net461" />
<package id="xunit.analyzers" version="0.8.0" targetFramework="net461" />
<package id="xunit.assert" version="2.3.1" targetFramework="net461" />
<package id="xunit.core" version="2.3.1" targetFramework="net461" />
<package id="xunit.extensibility.core" version="2.3.1" targetFramework="net461" />
<package id="xunit.extensibility.execution" version="2.3.1" targetFramework="net461" />
<package id="xunit.runner.visualstudio" version="2.3.1" targetFramework="net461" developmentDependency="true" />
</packages>
这是我的Class1.cs:
using OpenQA.Selenium.Chrome;
using Xunit;
namespace xUnitSpecFlowChrome
{
public class Class1
{
[Fact]
public void GoTest()
{
var options = new ChromeOptions();
options.AddArgument("--headless");
options.AddArgument("start-maximized");
options.AddArgument("--disable-gpu");
options.AddArgument("--disable-extensions");
var driver = new ChromeDriver(options);
driver.Navigate().GoToUrl("http://www.daringfireball.net");
var title = driver.Title;
}
}
}
这就是我在输出中运行该测试时看到的内容:
------ Run test started ------
[xUnit.net 00:00:00.5727852] Starting: xUnitSpecFlowChrome
[xUnit.net 00:00:03.3212989] xUnitSpecFlowChrome.Class1.GoTest [FAIL]
[xUnit.net 00:00:03.3239354] System.InvalidOperationException : session not created exception
from tab crashed
(Session info: headless chrome=65.0.3325.162)
(Driver info: chromedriver=2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91),platform=Windows NT 6.1.7601 SP1 x86_64) (SessionNotCreated)
[xUnit.net 00:00:03.3264983] Stack Trace:
[xUnit.net 00:00:03.3276332] at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
[xUnit.net 00:00:03.3280606] at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
[xUnit.net 00:00:03.3284914] at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
[xUnit.net 00:00:03.3289079] at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
[xUnit.net 00:00:03.3292786] at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
[xUnit.net 00:00:03.3296566] at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeOptions options)
[xUnit.net 00:00:03.3300055] C:\git\xUnitSpecFlowChrome\xUnitSpecFlowChrome\Class1.cs(14,0): at xUnitSpecFlowChrome.Class1.GoTest()
[xUnit.net 00:00:03.3573096] Finished: xUnitSpecFlowChrome
========== Run test finished: 1 run (0:00:04.757) ==========
更新
没有改变任何明显后果,我现在在输出窗格中看到了这一点:
------ Run test started ------
[xUnit.net 00:00:00.3376673] Starting: xUnitSpecFlowChrome
[xUnit.net 00:01:00.8905380] xUnitSpecFlowChrome.Class1.GoTest [FAIL]
[xUnit.net 00:01:00.9024429] OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL http://localhost:23698/session timed out after 60 seconds.
[xUnit.net 00:01:00.9038433] ---- System.Net.WebException : The operation has timed out
[xUnit.net 00:01:00.9185440] Stack Trace:
[xUnit.net 00:01:00.9199720] at OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)
[xUnit.net 00:01:00.9212636] at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
[xUnit.net 00:01:00.9224907] at OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute)
[xUnit.net 00:01:00.9237665] at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
[xUnit.net 00:01:00.9250241] at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
[xUnit.net 00:01:00.9262697] at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
[xUnit.net 00:01:00.9275900] at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
[xUnit.net 00:01:00.9289251] at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeOptions options)
[xUnit.net 00:01:00.9302787] C:\git\xUnitSpecFlowChrome\xUnitSpecFlowChrome\Class1.cs(16,0): at xUnitSpecFlowChrome.Class1.GoTest()
[xUnit.net 00:01:00.9315103] ----- Inner Stack Trace -----
[xUnit.net 00:01:00.9327543] at System.Net.HttpWebRequest.GetResponse()
[xUnit.net 00:01:00.9339867] at OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)
[xUnit.net 00:01:00.9698773] Finished: xUnitSpecFlowChrome
========== Run test finished: 1 run (0:01:01.085) ==========
答案 0 :(得分:0)
错误 System.InvalidOperationException:会话未创建异常表示无法创建会话。
要配置无头Chrome ,您必须通过 ChromeOptions 的实例传递以下参数:
var options = new ChromeOptions();
options.AddArgument("--headless");
options.AddArgument("start-maximized");
options.AddArgument("--disable-gpu");
options.AddArgument("--disable-extensions");
var driver = new ChromeDriver(options);
确保以下内容:
@Test
。由于您仍然看到错误为来自标签崩溃,因此此问题与系统/ docker_image中的内存不足有关。您可以在UnknownError: session deleted because of page crash from tab crashed
中找到详细的讨论答案 1 :(得分:0)
真的不能相信这就是它:
options.AddArgument("no-sandbox");
偶然发现: https://stackoverflow.com/a/39299877/71376
不知道为什么这个选项没有记录或标记更多 - 但我希望这有助于其他人。
答案 2 :(得分:0)
发布的答案对我不起作用-该测试在Chrome中有效,但不适用于--headless
选项参数。但是,它没有按预期工作,并且出现了超时错误。
此代码与最新版本一起使用...窗口大小对于无头模式非常重要。抬头模式的默认大小为1280x800
,无头模式的默认大小为1366x768
。
public void GoTest()
{
ChromeOptions options = new ChromeOptions();
options.AddArgument("--window-size=1920,1080");
options.AddArgument("--disable-gpu");
options.AddArgument("--disable-extensions");
options.AddArgument("--proxy-server='direct://'");
options.AddArgument("--proxy-bypass-list=*");
options.AddArgument("--start-maximized");
options.AddArgument("--headless");
options.AddArgument("no-sandbox");
var _driver = new ChromeDriver(options);
_driver.Navigate().GoToUrl("https://www.google.com");
object html = _driver.ExecuteScript("return document.body.parentElement.outerHTML");
_driver.Close();
}