How to close a web message from IE VBA? - excel

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.

Related

Clicking a button on Internet Explorer through Excel VBA

I am trying to click a button through Excel.
The focus is working. But the click does not.
Set objCollection = IE.document.getElementsByTagName("button")
While i < objCollection.Length
If objCollection(i).Type = "submit" And objCollection(i).innerText = "Add items" Then
Set objElement = objCollection(i)
objElement.Focus
objElement.Click
End If
i = i + 1
Wend
Unable to test...try to get it by id:
Set objElement = IE.document.getElementById("catalog.items.add")
objElement.Click
I made a quick test:
' Add references to the project:
' « Microsoft Internet Controls » and « Microsoft HTML Object Library »
Sub Test()
Dim ie, button As Object
Dim ht As HTMLDocument
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate ("https://www.sampleforms.com/")
Do Until ie.readyState = 4
DoEvents
Loop
Set ht = ie.document
ie.document.querySelector("#searchVal").Value = "yaya"
Set button = ie.document.querySelector(".search-btn")
button.Click
Do Until ie.readyState = 4
DoEvents
Loop
End Sub
So try with :
Set button = ie.document.querySelector("#catalog.items.add")

VBA DOM getElementsBy can't get childnodes

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

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

Looping through a row and copying each cell In a specific procedure

What I have to do is use Excel VBA to:
login to Amazon Seller
open a workbook
loop through a column to get an order number
put it in the search box
hit the search button
go to the order page and extract the data
then have the extracted data go back into a specified column in
another Excel workbook
The loop and order number parts are what I'm currently stumped on. I've figured out this much code as of this moment:
Sub MyAmazonSeller()
Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
Dim oSignInLink As HTMLLinkElement
Dim oInputEmail As HTMLInputElement
Dim oInputPassword As HTMLInputElement
Dim oInputSigninButton As HTMLInputButtonElement
'InputSearchOrder will be the destination for order numbers taken from the workbook
Dim InputSearchOrder As HTMLInputElement
Dim InputSearchButton As HTMLInputButtonElement
Dim IE As InternetExplorer
Dim AAOrder As Workbook
Dim AAws As Worksheet
MyURL = "https://sellercentral.amazon.com/gp/homepage.html"
Set IE = New InternetExplorer
' Open the browser and navigate.
With IE
.Silent = True
.Navigate MyURL
.Visible = True
Do
DoEvents
Loop Until .ReadyState = READYSTATE_COMPLETE
End With
' Get the html document.
Set HTMLDoc = IE.Document
' See if you have the sign in link is because you are in the main
' page
Set oSignInLink = HTMLDoc.getElementById("signin-button-container")
If Not oSignInLink Is Nothing Then
oSignInLink.Click
Do
DoEvents
Loop Until IE.ReadyState = READYSTATE_COMPLETE
End If
' Get the email field and the next button
Set oInputEmail = HTMLDoc.getElementById("username")
Set oInputPassword = HTMLDoc.getElementById("password")
' Click the button and wait
oInputEmail.Value = "xxxxxx#xxxxxx.net"
' Get the password field and the sign in button
Set oInputPassword = HTMLDoc.getElementById("password")
Set oInputSigninButton = HTMLDoc.getElementById("sign-in-button")
' Click the button and wait
oInputPassword.Value = "xxxxxxxx"
oInputSigninButton.Click
Do
DoEvents
Loop Until IE.ReadyState = READYSTATE_COMPLETE
Application.Wait (Now + TimeValue("0:00:05"))
Set AAOrder = Application.Workbooks.Open("Z:\Employee Folders\Employee\trackingnumber_sample_spreadsheet.xls")
Set AAws = AAws.Worksheets("PrimeOrdersWithNoFulfillmentRe")
Set InputSearchOrder = HTMLDoc.getElementById("sc-search-field")
'What I'm currently stuck on
InputSearchOrder.Value = "001-7163923-7572632"
Set InputSearchButton = HTMLDoc.getElementsByClassName("sc-search-button")(0)
InputSearchButton.Click
Do
DoEvents
Loop Until IE.ReadyState = READYSTATE_COMPLETE
'Was able to add this snippet, but I'm getting an error 13, most likely with
'my e variable. I'm basically trying to do a loop within a loop, extracting 5
'pieces of data and sticking them back into their respective columns in the
'original Excel sheet. The problem comes when scraping the HTML. I'm basically
'trying to get text in the tables which have a few levels and it's frustrating
'me to no end.
With HTMLDoc
Set elems = HTMLDoc.getElementsByTagName("td")
For Each e In elems
If e.innerText Like "*1Z*" Then
Range("D2").Value = e.innerText
End If
Next e
End With
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub

vba code to fetch data from website

I am a newbie in this website and in VBA programming as well. I am stuck into a problem where I have to fetch the data from this page. I need to have the hyperlink url of Check Rates 10 button. Can anyone help me with this problem.
I have done the following code:
Sub GetData()
Dim IE As New InternetExplorer
IE.navigate "http://www.kieskeurig.nl/zoeken/index.html?q=4960999543345"
IE.Visible = False
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Application.Wait (Now() + TimeValue("00:00:016")) ' For internal page refresh or loading
Dim doc As HTMLDocument 'variable for document or data which need to be extracted out of webpage
Set doc = IE.document
Dim dd As Variant
dd = doc.getElementsByClassName("lgn")(0).outerHtml
'Range("a1").Value = dd
MsgBox dd
End Sub
In which I am getting text of the button but I want to have the value of the class. I think I am very close to the result but somehow cant reach to the goal...can anyone please help me...
Regards,
I think this is what you're looking for:
(Code modified slightly from Kyle's answer here)
Sub Test()
'Must have the Microsoft HTML Object Library reference enabled
Dim oHtml As HTMLDocument
Dim oElement As Object
Dim link As String
Set oHtml = New HTMLDocument
With CreateObject("WINHTTP.WinHTTPRequest.5.1")
.Open "GET", "http://www.kieskeurig.nl/zoeken/index.html?q=4960999543345", False
.Send
oHtml.Body.innerHTML = .responseText
End With
If InStr(1, oHtml.getElementsByClassName("lgn")(0).innerText, "Bekijk 10 prijzen") > 0 Then
link = Mid(oHtml.getElementsByClassName("lgn")(0).href, 7)
Debug.Print "http://www.kieskeurig.nl" & link
End If
End Sub
This code prints the URL to the immediate window. Hope that helps!
This works for me...
Sub GetData()
Set IE = CreateObject("InternetExplorer.Application")
my_url = "http://www.kieskeurig.nl/zoeken/index.html?q=4960999543345"
With IE
.Visible = True
.navigate my_url
.Top = 50
.Left = 530
.Height = 400
.Width = 400
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
End With
Application.Wait (Now() + TimeValue("00:00:016")) ' For internal page refresh or loading
Set Results = IE.document.getElementsByTagName("a")
For Each itm In Results
If itm.classname = "lgn" Then
dd = itm.getAttribute("href")
Exit For
End If
Next
' if you wnat to click the link
itm.Click
' otherwise
'Range("a1").Value = dd
MsgBox dd
End Sub

Resources