Skip to content Skip to sidebar Skip to footer

Store Selenium Html Source Code Into Element Of Type Htmldocument

Is it possible to store the HTML source grabbed with Selenium (using Excel VBA) into a HTMLDocument element? This is an example using Microsoft Internet Controls and Microsoft HTM

Solution 1:

The proper way to get the DOM with SeleniumBasic:

Sub Get_DOM()
  Dim driver AsNew FirefoxDriver
  driver.Get"https://en.wikipedia.org/wiki/Main_Page"Dim html AsNew HTMLDocument  ' Requires Microsoft HTML Library
  html.body.innerHTML = driver.ExecuteScript("return document.body.innerHTML;")

  Debug.Print html.body.innerText

  driver.Quit
EndSub

To get the latest version in date working with the above example: https://github.com/florentbr/SeleniumBasic/releases/latest

Solution 2:

This should work to use a string as the source for an HTML document:

Set html =New HTMLDocument
html.body.innerHTML = selenium.pageSource

edit: changed Selenium call to pageSource from getHtmlSource. Full working code as follows. Not sure that we're using the same version of Selenium though:

OptionExplicitSub foo()

Dim sel As selenium.WebDriver
Set sel = New selenium.WebDriver
Dim html As HTMLDocument

sel.Start "firefox", "about:blank"
sel.Get"http://www.google.com/"Set html = New HTMLDocument
html.body.innerHTML = sel.PageSource

Debug.Print html.body.innerText

EndSub

with references to Microsoft HTML Object Library and Selenium Type Library (Selenium32.tlb) - using SeleniumBasic version 2.0.6.0

Solution 3:

Not quite sure why you prefer converting an Selenium element to a HTMLDocument. It'd require one more bounded dependency to your project.

Personally I prefer allocating DOM-element to a WebElement. For instance:

If (Selenium.FindElementsByClass("qty").Count > 0) ThenDim qtyElement as WebElement: Set qtyElement = Selenium.FindElementByClass("qty")
EndIfIf (Not qtyElement isNothing) thenDim qtyHtml asString: qtyHtml = qrtElement.Attribute("innerHTML")
Endif

Debug.Print qtyHtml 

Post a Comment for "Store Selenium Html Source Code Into Element Of Type Htmldocument"