Chorme设置为无头,设置不加载图片无效

时间:2018-12-26 03:53:57

标签: c# webdriver selenium-chromedriver

设置options.AddArgument("--headless");

无效的执行代码

options.AddUserProfilePreference("profile", 
new { default_content_setting_values = new { images = 2, javascript = 2 } });

C#代码

        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);
        }

**测试HTML代码**

<!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://www.bing.com/az/hprichbg/rb/ToyXmasTree_EN-US5514346051_800x480.jpg"/>
        </div>
        <script type="text/javascript">
            document.getElementById('test').innerHTML += "</br>This is javascrtip test";
        </script>
    </body>
    </html>

网页截图

set headless

如果我注释它 //options.AddArgument(“-headless”);

Not set up headless

图片和JavaScript将不会显示

0 个答案:

没有答案