1

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

anagnam
  • 143
  • 1
  • 9
  • 2
    The HTML Agility pack doesn't do this. You would have to find some other library, like [Awesomium](http://www.awesomium.com/). – George Duckett Nov 17 '14 at 11:34
  • 1
    If you want to capture your screen, please take a look at this: http://stackoverflow.com/questions/3072349/capture-screenshot-including-semitransparent-windows-in-net/3072580#3072580 HTML Agility pack has nothing to do with it – YuriG Nov 17 '14 at 11:38

2 Answers2

4

You can't do this with HTML Agility Pack. Use a different tool such as Selenium WebDriver. Here is how to do it: Take a screenshot with Selenium WebDriver

Community
  • 1
  • 1
Svein Fidjestøl
  • 2,359
  • 2
  • 17
  • 39
4

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();
        }
    }
}
Richard Ev
  • 48,781
  • 54
  • 181
  • 273
  • 1
    your code works now. but is there a way the Firefox browser will not popup but instead just take screenshot in the background while HtmlAgilityPack is doing its thing? – anagnam Nov 17 '14 at 12:50
  • @reymangana - nope. Selenium WebDriver manages the Firefox session. It is able to scrape information from a webpage though, so I suspect you can use it for whatever you're currently doing using the Agility Pack. – Richard Ev Nov 17 '14 at 14:12