我有一个简单的F#项目,它使用 Selenium 和 Chrome 。我注意到,当Chrome在无头模式下启动时,Chrome对象未定义。
另一方面,如果我“正常”运行它,则正确引用chrome对象。
示例代码:
let chromeOptions = new ChromeOptions(BinaryLocation = _chrome)
chromeOptions.AddArguments
(
"--headless",
"--disable-gpu"
)
let chrome = new ChromeDriver(chromeOptions, Url = "http://www.example.com")
try
Console.WriteLine(chrome.Capabilities.Platform)
Console.WriteLine(chrome.Capabilities.BrowserName)
Console.WriteLine(chrome.Capabilities.Version)
let res = chrome.ExecuteScript("return chrome;")
Console.WriteLine(res)
with e ->
Console.WriteLine(e.Message)
chrome.Quit()
如果我运行上面的代码,我得到以下输出:
Starting ChromeDriver 2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8) on port 61158
Only local connections are allowed.
Any
chrome
62.0.3188.0
unknown error: chrome is not defined
(Session info: headless chrome=62.0.3188.0)
(Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 10.0.15063 x86_64)
如果省略 headless 选项,我得到以下输出:
Starting ChromeDriver 2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8) on port 60248
Only local connections are allowed.
Any
chrome
62.0.3188.0
System.Collections.Generic.Dictionary`2[System.String,System.Object]
这是一个错误还是我遗失了什么?