using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using System; using System.IO; using System.Reflection; using System.Text; using System.Threading; namespace HTEXChart { class Program { static void Main(string[] args) { //ChromeOptions op = new ChromeOptions(); //op.AddArguments("--headless");//开启无gui模式 //op.AddArguments("--no-sandbox");//停用沙箱以在Linux中正常运行 //ChromeDriver cd = new ChromeDriver(Environment.CurrentDirectory, op, TimeSpan.FromSeconds(180)); //cd.Navigate().GoToUrl("http://chart.icaile.com/sd11x5.php"); //string text = cd.FindElementById("fixedtable").Text; //cd.Quit(); PageScreenshotAll("https://cdhabook.teammodel.cn/ziteng/index.html", "F:\\111.png"); //Console.WriteLine(text); // Console.Read(); } public static void PageScreenshotAll(string url, string path) { ChromeDriver driver = null; try { ChromeOptions options = new ChromeOptions(); options.AddArguments("headless", "disable-gpu"); driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), options); driver.Navigate().GoToUrl(url); string width = driver.ExecuteScript("return document.body.scrollWidth").ToString(); string height = driver.ExecuteScript("return document.body.scrollHeight").ToString(); driver.Manage().Window.Size = new System.Drawing.Size(int.Parse(width)+40, int.Parse(height)); //=int.Parse( height); var screenshot = (driver as ITakesScreenshot).GetScreenshot(); screenshot.SaveAsFile(path); } catch (Exception ex) { // logger.Error(ex.Message + Environment.NewLine + ex.StackTrace); } finally { if (driver != null) { driver.Close(); driver.Quit(); } } } public static void PageScreenshot(string url, string path) { ChromeDriver driver = null; try { ChromeOptions options = new ChromeOptions(); options.AddArguments("headless", "disable-gpu"); driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), options, TimeSpan.FromSeconds(10)); driver.Navigate().GoToUrl(url); var screenshot = (driver as ITakesScreenshot).GetScreenshot(); screenshot.SaveAsFile(path); //driver.Navigate().GoToUrl("F:\\ECharts1\\index.html"); //var screenshot1 = (driver as ITakesScreenshot).GetScreenshot(); //screenshot1.SaveAsFile("F:\\222.png"); } catch (Exception ex) { // logger.Error(ex.Message + Environment.NewLine + ex.StackTrace); } finally { if (driver != null) { driver.Close(); driver.Quit(); } } } } }