0

in this post I try to reformulate my previous questions. I am trying to extract in excel a table in a webpage. The problem is that the webpage is generated using javascript so when I look at the page source I do not find where the table is defined. What I was trying to do is:

  1. Load the page
  2. Select from a first menu on the left the option "Calcio" and then "ITA Serie A" (sorry but the webpage is in Italian)
  3. Extract the data in the table using "standard" functions such as getElementsByTagName or getElementByID

Up to now I was able to load the page, run a script for step 2 (and the page is correctly updated). The problem is that when I look at the page source I am not able to find the table I am interested in (the one with the Header "ESITO FINALE 1X2") so I do not know how to proceed to import the table in Excel.

The starting page url is : "https://www.sisal.it/scommesse-matchpoint".

My goal is to import the data of the table into excel so if there is a completely different approach to solve the problem I am open to it. Thanks!

Sub Control_Sisal()

  Dim htmlPage          As htmlDocument
  Dim strUrl            As String
  Const Title As String = "scommesse"

  strUrl = "https://www.sisal.it/scommesse-matchpoint"

  Call Navigate_Sisal(strUrl, htmlPage, Title)

End Sub

Sub Navigate_Sisal(strUrl As String, htmlPage As htmlDocument, Title As String)

  Dim IE            As Object
  Dim strScript     As String

  Set IE = CreateObject("InternetExplorer.application")  '
  IE.Visible = True
  IE.navigate strUrl

  Do 
     DoEvents 
  Loop Until IE.ReadyState = READYSTATE_COMPLETE 
  '
  ' Run the scripts to get the data
  '
  strScript = "getAlberaturaAntepostManager().clickManifestazione(1, 21)"
  IE.Document.parentWindow.execScript strScript, "jscript"

  Do 
    DoEvents 
  Loop Until IE.ReadyState = READYSTATE_COMPLETE

  Set htmlPage = IE.Document

End Sub
Community
  • 1
  • 1
MeSS83
  • 349
  • 2
  • 7
  • 20
  • 1
    Use your web browser's Developer tools (F12) to view the final generated HTML - you should be able to find what you need there. That URL is unreachable for me, so can't be more specific. – Tim Williams Sep 10 '15 at 18:29
  • Thanks Tim, I did something similar to get the instruction "getAlberaturaAntepostManager().clickManifestazione(1, 21)". So now my question is, if I use the last command in the code `Set htmlPage = IE.Document` what contains htmlPage? I mean if in htmlPage I will found everything that I see using F12 or just the few things that I see looking at the page source. Sorry if the question is easy but I am not that good – MeSS83 Sep 10 '15 at 18:51
  • 1
    The `document` object represents the full page at that point in time, including any content generated by script (assuming that is completely rendered). It isn't necessarily the same as what you see using "view source". – Tim Williams Sep 10 '15 at 18:55
  • Thanks Tim it worked!! If I can I have a corollary question: sometimes the script instruction has still to be executed when i import the data in the object htmlPage. Is there any "elegant" solution to avoid this problem? For the moment I add a timer like in this post [VBA Internet Explorer wait for web page to load](http://stackoverflow.com/questions/19933313/vba-internet-explorer-wait-for-web-page-to-load) – MeSS83 Sep 10 '15 at 19:41
  • A timer will work, or you could create a loop which checks for the existence of a particular element which is part of the content you're waiting for, and exit the loop when it is found. E.g. http://stackoverflow.com/questions/12822749/logic-to-loop-until-web-page-element-is-not-nothing – Tim Williams Sep 10 '15 at 19:56

1 Answers1

0

This answer is the one given by @TimWilliams in the above comments. I wrote the answer here to close the question and help beginners like me.

The document object represents the full page at that point in time, including any content generated by script (assuming that is completely rendered). It isn't necessarily the same as what is displayed using "view source". Using the browser's Developer tools (F12) it is possible to view the final generated HTML and to find the elements needed (the script-generated table for the specific case of my question).

MeSS83
  • 349
  • 2
  • 7
  • 20