Screen Capture in C# using HtmlAgilityPack

c# html-agility-pack

Question

Due to the lack of proper documentation, I'm not sure if HtmlAgilityPack supports screen capture in C# after it loads the html contents.

So is there a way I can more or less grab a screenshot using (or along with) HtmlAgilityPack so I can have a visual clue as to what happens every time I do page manipulations?

Here is my working code so far:

using HtmlAgilityPack;
using System;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            string urlDemo = "https://htmlagilitypack.codeplex.com/";
            HtmlWeb getHtmlWeb = new HtmlWeb();
            var doc = getHtmlWeb.Load(urlDemo);
            var sentence = doc.DocumentNode.SelectNodes("//p");
            int counter = 1;           
            try
            {
                foreach (var p in sentence)
                {
                    Console.WriteLine(counter + ". " + p.InnerText);
                    counter++;
                }
            }
            catch (Exception e)
            { 
                Console.WriteLine(e);
            }  
            Console.ReadLine();            
        }
    }
}

Currently, it scrapes and output all the p of the page in the console but at the same time I want to get a screen grab of the scraped contents but I don't know how and where to begin.

Any help is greatly appreciated. TIA

1
1
11/17/2014 11:30:47 AM

Popular Answer

Could you use Selenium WebDriver instead?

You'll need to add the following NuGet packages to your project first:

Loading a page and taking a screenshot is then as simple as...

using System;
using System.Drawing.Imaging;
using System.IO;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace SeleniumTest
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a web driver that used Firefox
            var driver = new FirefoxDriver(
                new FirefoxBinary(), new FirefoxProfile(), TimeSpan.FromSeconds(120));

            // Load your page
            driver.Navigate().GoToUrl("http://google.com");

            // Wait until the page has actually loaded
            var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 10));
            wait.Until(d => d.Title.Contains("Google"));

            // Take a screenshot, and saves it to a file (you must have full access rights to the save location).
            var myDesktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            ((ITakesScreenshot)driver).GetScreenshot().SaveAsFile(Path.Combine(myDesktop, "google-screenshot.png"), ImageFormat.Png);

            driver.Close();
        }
    }
}
4
11/17/2014 12:30:10 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow