Search code examples
c#vb.netseleniumhtml-agility-pack

How to get HtmlAgilityPack.HtmlDocument from Selenium Driver.PageSource?


I'm trying to use HtmlAgilityPack with Selenium. I want to make some test, but don't know how to load HtmlDocument from Selenium Driver.PageSource String. Any help? (c# or vb.net)

Here the code...

Dim driver As IWebDriver
Dim ChromeOptions As New ChromeOptions
driver = New ChromeDriver("C:\ChromeDriver", ChromeOptions)
driver.Navigate.GoToUrl("www.Google.com")

Dim doc As New HtmlDocument
Dim wb As New HtmlWeb
doc = wb.LoadFromBrowser(driver.PageSource)

N.B. My Question regard interaction between Selenium and HtmlAgilityPack.


Solution

  • I found the solution: When we want to interact between Selenium and HtmlAgilityPack, we don't need to create an instance of HtmlWeb, because we have already the Selenium Browser. So just load the HtmlDocument directly from the Driver.PageSource:

    Dim driver As IWebDriver
    Dim ChromeOptions As New ChromeOptions
    driver = New ChromeDriver("C:\ChromeDriver", ChromeOptions)
    driver.Navigate.GoToUrl("www.Google.com")
    
    Dim doc As New HtmlDocument
    doc.LoadHtml(driver.PageSource)
    

    Since there are not many similar help on internet, regarding interaction between Selenium and HtmlAgilityPack, i publish myself the answer, maybe can be in help.