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

c# html-agility-pack selenium vb.net

Question

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.

Popular Answer

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.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why