ChromeOptions options = new ChromeOptions();
options.AddArgument("--headless");
options.AddArgument("--window-size=1440,900");
options.AddUserProfilePreference("profile", new { default_content_setting_values = new { images = 2, javascript = 2 } });
var service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;
using (IWebDriver driver = new ChromeDriver(service, options))
{
var savepath = Environment.CurrentDirectory;
driver.Navigate().GoToUrl(savepath+ "\\test.html");
var savepathname = savepath + "\\Img\\test1.png";
Screenshot screenShotFile = ((ITakesScreenshot)driver).GetScreenshot();
screenShotFile.SaveAsFile(savepathname, ScreenshotImageFormat.Png);
Thread.Sleep(100000);
}
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>test html</title>
</head>
<body>
<div id="test">
This is test
</div>
<div>
<img src="https://img.uj5u.com/2020/09/21/115093211008051.jpg"/>
</div>
<script type="text/javascript">
document.getElementById('test').innerHTML += "</br>This is javascrtip test";
</script>
</body>
</html>
用無頭請求網頁,并且截圖。但是我禁止了圖片和JavaScript,在無頭中,它依然執行
截圖圖片

我注釋了代碼后
//options.AddArgument("--headless");
然后截圖就是下面這圖片。

請問,怎么在無頭模式下,禁用JS和圖片???
uj5u.com熱心網友回復:
options.AddArgument("--headless");// 禁用圖片
options.AddUserProfilePreference("profile.default_content_setting_values.images", 2);
我這么寫的無頭模式下是可以禁用圖片的
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/96210.html
標籤:C#
