VBA DOM getElementsBy can't get childnodes - excel

I'm trying to get the innertext of a label but i'm getting an error. Through the console i'm succesfully getting the inner text with this script :
document.getElementsByClassName("item alt")[0].childNodes[2].childNodes[0].innerText
Element i'm trying to get :
<tr class="item alt" data-id="1376936"><td class="toolbar left"><span class="ui-icon ui-icon-triangle-1-e"></span></td><td class="time">14:00</td><td class="status"><span class="status-1 rc">FT</span>
My VBA script :
Sub WebScraping()
Dim ie As InternetExplorer
Dim html As HTMLDocument
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate "https://www.whoscored.com/Regions/74/Tournaments/22/Seasons/7814/Stages/17593/Fixtures/France-Ligue-1-2019-2020"
Do While ie.readyState <> READYSTATE_COMPLETE
Application.StatusBar = "Trying to go to Whoscored ..."
DoEvents
Loop
Set doc = ie.document
Do While ie.readyState <> READYSTATE_COMPLETE
Application.StatusBar = "Trying to go to Whoscored ..."
DoEvents
Loop
Set a = doc.getElementsByClassName("item alt")(0).ChildNodes(2).ChildNodes(0).innerText
MsgBox (a)
End Sub

Set a = doc.getElementsByClassName("item alt")(0).ChildNodes(2).ChildNodes(0).innerText
Try to use the getElementsByClasssName method to find the child node, please modify above code as below:
Dim a As String
a = doc.getElementsByClassName("item alt")(0).getElementsByClassName("status")(0).getElementsByClassName("status-1")(0).innerText
MsgBox (a)

The first line in every module should be Option Explicit.
I'm not sure what you want at all. But to show the wanted element use this:
Sub WebScraping()
Dim ie As InternetExplorer
Dim doc As HTMLDocument
Dim a As Object
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate "https://www.whoscored.com/Regions/74/Tournaments/22/Seasons/7814/Stages/17593/Fixtures/France-Ligue-1-2019-2020"
Do While ie.readyState <> READYSTATE_COMPLETE
Application.StatusBar = "Trying to go to Whoscored ..."
DoEvents
Loop
Application.StatusBar = False
Set doc = ie.document
Set a = doc.getElementsByClassName("item alt")(0).getElementsByClassName("status")(0)
MsgBox a.innerText
End Sub

Related

How to close a web message from IE VBA?

I,ve been dealing with a issue during my web scraping.
My problem is that when I click to "Submit" a form, it pops-up a web message that I've not been able to close.
I´ve already try the sendKeys Method but no success.
here is the link of the web page:
https://www3.bcb.gov.br/CALCIDADAO/publico/corrigirPorIndice.do?method=corrigirPorIndice
Here is a print of the info to put in the webpage
Here is a print of the info to put in the webpage
The message appers when you click the button on the left "Corrigir Valor"
enter image description here
the messege
enter image description here
obs. pressing enter or esc with the keyboard close it, but I`m not been able to do it in the code
here is the code if may be helpful
Sub atualizacao_valores()
Dim data_base As String
Dim data_atualizacao As String
Dim valor_face As String
Dim drp As HTMLFormElement
Dim html As HTMLDocument
Set ie = CreateObject("internetexplorer.application")
ie.navigate "https://www3.bcb.gov.br/CALCIDADAO/publico/exibirFormCorrecaoValores.do?method=exibirFormCorrecaoValores&aba=1"
ie.Visible = True
Do While ie.busy And ie.readyState <> "READYSTATE_COMPLETE"
DoEvents
Loop
data_base_ipcae = "01/2022"
data_atualizacao_ipcae = "12/2022"
valor_face = "100000"
Application.Wait (Now + TimeValue("00:00:02"))
Set html = ie.document
Set drp = html.getElementById("selIndice")
drp.selectedIndex = 4
ie.document.getelementsbytagname("input")(1).Value = data_base_ipcae
ie.document.getelementsbytagname("input")(2).Value = data_atualizacao_ipcae
ie.document.getelementsbytagname("input")(3).Value = valor_face
ie.document.getelementsbyclassname("botao")(0).Click
`here happens the pop up that I cant close
...
end sub
I agree with the suggestion given by Tim Williams.
Adding code below just above the line when you click the button could suppress the Alert().
With html.parentWindow
.execScript "window.alert = function(){return true;};", "JScript"
End With
Your modified code:
Sub atualizacao_valores()
Dim data_base As String
Dim data_atualizacao As String
Dim valor_face As String
Dim drp As HTMLFormElement
Dim html As HTMLDocument
Set ie = CreateObject("internetexplorer.application")
ie.navigate "https://www3.bcb.gov.br/CALCIDADAO/publico/exibirFormCorrecaoValores.do?method=exibirFormCorrecaoValores&aba=1"
ie.Visible = True
Do While ie.busy And ie.readyState <> "READYSTATE_COMPLETE"
DoEvents
Loop
data_base_ipcae = "01/2022"
data_atualizacao_ipcae = "12/2022"
valor_face = "100000"
Application.Wait (Now + TimeValue("00:00:02"))
Set html = ie.document
Set drp = html.getElementById("selIndice")
drp.selectedIndex = 4
ie.document.getelementsbytagname("input")(1).Value = data_base_ipcae
ie.document.getelementsbytagname("input")(2).Value = data_atualizacao_ipcae
ie.document.getelementsbytagname("input")(3).Value = valor_face
With html.parentWindow
.execScript "window.alert = function(){return true;};", "JScript"
End With
ie.document.getelementsbyclassname("botao")(0).Click
End Sub
I have tested this code on my end and it is suppressing the Alert message.
You could test it and let us know your test results.

Extract html source code into excel using VBA

I am trying to simply paste the content or innertext into excel using getElementByID function.
The content is actually the iframe link which I am trying to extract it and paste into cell.
The photo shown is the html source code.
Sub GetData()
Dim ie As New SHDocVw.InternetExplorer
Dim htmldoc As MSHTML.HTMLDocument
Dim result As MSHTML.IHTMLElement
ie.Visible = True
ie.navigate "http://www.bursamalaysia.com/market/listed-companies/company-announcements/5925865"
Do While ie.readyState <> READYSTATE_COMPLETE
Loop
Application.Wait (Now() + TimeValue("00:00:016")) ' For internal page refresh or loading
Set htmldoc = ie.document
Set Results = HTML.getElementById("bm_ann_detail_iframe")
Sheets("Sheet1").Range("a1").Value = Results.innerText
End Sub
html source code
You should use consistent variable naming in your code. If you put Option Explicit at the top of your code that will help.
You want to access the src attribute of the iframe to get the URL shown.
If you plan to use the new URL then you actually want the part before the "#". This means changing to:
ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = Split(ie.document.getElementById("bm_ann_detail_iframe").src, "#")(0)
Code:
Option Explicit
Public Sub GetData()
Dim ie As New SHDocVw.InternetExplorer
ie.Visible = True
ie.navigate "http://www.bursamalaysia.com/market/listed-companies/company-announcements/5925865"
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
ThisWorkbook.Worksheets("Sheet1") = ie.document.getElementById("bm_ann_detail_iframe").src
ie.Quit
End Sub

vba get text from webpage and display as message in excel?

I am using the following code in vba to extract a value from a website.
Let's pretend the value is wrapped inside a html span id. i want to retrieve this value and display it as a message box in excel.
Here's my code:
Sub PullExpiry()
Dim IE As Object
' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
' You can uncoment Next line To see form results
IE.Visible = False
' URL to get data from
IE.Navigate "https://www.brcdirectory.com/InternalSite//Site.aspx?BrcSiteCode=" & Range("J6").Value
' Statusbar
Application.StatusBar = "Loading, Please wait..."
' Wait while IE loading...
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Application.StatusBar = "Searching for value. Please wait..."
Dim dd As String
dd = IE.Document.getElementsByClassName("ctl00_ContentPlaceHolder1_FormView1_GridView1_ctl02_lb_ExpiryDate")(0).innerText
MsgBox dd
' Show IE
IE.Visible = True
' Clean up
Set IE = Nothing
Application.StatusBar = ""
End Sub
HTML:
<span id="ctl00_ContentPlaceHolder1_FormView1_GridView1_ctl02_lb_ExpiryDate">Expiry Date : 16/02/2018</span>
I get an object doesn't support this property or method error:
IE.Document.getElementsByClassName("ctl00_ContentPlaceHolder1_FormView1_GridView1_ctl02_lb_ExpiryDate")(0).innerText
Please can someone show me where i am going wrong?
EDIT
Other code tried:
Sub PullExpiry()
Dim IE As Object
' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
' You can uncoment Next line To see form results
IE.Visible = False
' URL to get data from
IE.Navigate "https://www.brcdirectory.com/InternalSite//Site.aspx?BrcSiteCode=" & Range("J6").Value
' Statusbar
Application.StatusBar = "Loading, Please wait..."
' Wait while IE loading...
Do While IE.Busy
DoEvents
Loop
Application.StatusBar = "Searching for value. Please wait..."
Dim dd As String
dd = IE.Document.getElementByID("ctl00_ContentPlaceHolder1_FormView1_GridView1_ctl02_lb_ExpiryDate")(0).innerText
MsgBox dd
' Show IE
IE.Visible = True
' Clean up
Set IE = Nothing
Application.StatusBar = ""
End Sub

Excel VB Macro to scrape webpage. Can't code to click html button

I have a short excel macro that is designed to:
1) Open Internet Explorer and navigate to "http://www.puco.ohio.gov/pucogis/address/search.cfm"
2) Fill out a form on that site with data from the excel workbook
3) Click a button to submit the form
4) Scrape some innertext from the website and place it in a cell in the workbook
5) Close Internet Explorer
I can not get step 3 to work. That is, I can not get the click/submit function to work with this website. When the button is clicked the website populates with information specific to the information entered in the form. Everything else in the code is working. I have searched for an answer and tried the submit verses click approach with no luck.
Thanks for you help.
Code below:
Private Sub SiteData()
Dim ie As Object
Dim utility As Variant
Dim HTMLButton
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "http://www.puco.ohio.gov/pucogis/address/search.cfm"
ie.Visible = True
While ie.Busy
DoEvents
Wend
ie.Document.all("address").Value = ThisWorkbook.Sheets("Site Info").Range("D14")
While ie.Busy
DoEvents
Wend
Set HTMLButton = ie.Document.getElementsByTagName("input")(1)
HTMLButton.Click
While ie.Busy
DoEvents
Wend
Set utility = ie.Document.getElementById("supName")
ThisWorkbook.Sheets("Site Info").Range("D50") = utility.innerText
ie.Quit
Set ie = Nothing
End Sub
Try this solution, which I found from this answer to a similar question. That answer was not accepted, but I have tested this with your code and seems to be working.
Private Sub SiteData()
Dim ie As Object
Dim utility As Variant
Dim HTMLButton
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "http://www.puco.ohio.gov/pucogis/address/search.cfm"
ie.Visible = True
While ie.Busy
DoEvents
Wend
ie.Document.all("address").Value = ThisWorkbook.Sheets("Site Info").Range("D14")
While ie.Busy
DoEvents
Wend
Call ie.Document.parentWindow.execScript("codeAddress()")
While ie.Busy
DoEvents
Wend
Set utility = ie.Document.getElementById("supName")
ThisWorkbook.Sheets("Site Info").Range("D50") = utility.innerText
ie.Quit
Set ie = Nothing
End Sub
If you don't know or can't reasonably anticipate the function call codeAddress(), then you can try something like this to derive it from the button's onclick property:
Dim fn$
fn = HTMLButton.onclick
fn = Mid(fn, InStr(fn, "{"))
fn = Trim(Replace(Replace(Replace(fn, "{", vbNullString), "}", vbNullString), vbLf, vbNullString))
Call ie.Document.parentWindow.execScript(fn)
You can call the JavaScript directly. try this it will work
Instead of:
Set HTMLButton = ie.Document.getElementsByTagName("input")(2)
HTMLButton.Click
use
ie.Document.parentWindow.execScript code:="codeAddress()"
note that IE may prompt you to confirm every run so you may need to
stop showing this message for smooth operation
Private Sub CommandButton1_Click()
Dim ie As Object
Dim utility As Variant
Dim HTMLButton
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "http://www.puco.ohio.gov/pucogis/address/search.cfm"
ie.Visible = True
While ie.Busy
DoEvents
Wend
ie.Document.all("address").Value = ThisWorkbook.Sheets("Site Info").Range("D14")
While ie.Busy
DoEvents
Wend
ie.Document.parentWindow.execScript code:="codeAddress()"
'Set HTMLButton = ie.Document.getElementsByTagName("input")(2)
'HTMLButton.Click
While ie.Busy
DoEvents
Wend
Set utility = ie.Document.getElementById("supName")
ThisWorkbook.Sheets("Site Info").Range("D16") = utility.innerText
ie.Quit
Set ie = Nothing
End Sub
thanks also to this article helped me to solve your problem
How to find and call javascript method from vba

VBA Scraping Data From Multiple Websites

I am trying to use VBA to scrape commodity/stock prices from multiple pages on investing.com and insert them into an excel spreadsheet.
The following code is what I have working to do a single price, in this example gold:
Sub Extractdatafromwebsite()
Dim ie As New InternetExplorer
Dim doc As HTMLDocument
ie.Visible = False
ie.navigate "http://uk.investing.com/commodities/gold"
Do
DoEvents
Loop Until ie.READYSTATE = READYSTATE_COMPLETE
Set doc = ie.document
output = doc.GetElementById("last_last").innerText
Range("A1").Value = output
ie.Quit
End Sub
However I need data from multiple sites to get different prices, all at the same time.
I tried elaborating on the code I have that is working, the following example is what I tried to display the price of gold and silver, however it only displayed the gold price in cells A1 & A2:
Sub Extractdatafromwebsite()
Dim ie As New InternetExplorer
Dim doc As HTMLDocument
ie.Visible = False
ie.navigate "http://uk.investing.com/commodities/gold"
Do
DoEvents
Loop Until ie.READYSTATE = READYSTATE_COMPLETE
Set doc = ie.document
output = doc.GetElementById("last_last").innerText
Range("A1").Value = output
ie.Quit
ie.navigate "http://uk.investing.com/commodities/silver"
Set doc = ie.document
output = doc.GetElementById("last_last").innerText
Range("A2").Value = output
ie.Quit
End Sub
Please could somebody help me figure out how to get this to work for multiple pages? I have tried searching however have not come up with anything that suits my needs.
Also is it possible to get something to pop up saying something like "Waiting...." whilst the data is being collected?
Thanks
I've found that using READYSTATE is not reliable in the sense that the document hasn't fully loaded sometimes - or at least the object model hasn't loaded.
So I typically add a sleep command and Doevents before trying to access the new doc object
This should work for you (and as #Dave says, you don't need to use IE.Quit)
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub Extractdatafromwebsite()
Dim ie As New InternetExplorer
Dim doc As HTMLDocument
ie.Visible = False
ie.Navigate "http://uk.investing.com/commodities/gold"
Do
Sleep 500
DoEvents
Loop Until ie.ReadyState = 4 ' READYSTATE_COMPLETE
Sleep 500
Set doc = ie.Document
output = doc.GetElementById("last_last").innerText
Range("A1").Value = output
ie.Navigate "http://uk.investing.com/commodities/silver"
Do
Sleep 500
DoEvents
Loop Until ie.ReadyState = 4 ' READYSTATE_COMPLETE
Sleep 500
Set doc = ie.Document
output = doc.GetElementById("last_last").innerText
Range("A2").Value = output
ie.Quit
Set ie = Nothing
End Sub

Resources