Clicking a button on Internet Explorer through Excel VBA - excel

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")

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.

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

How is it possible to click on a button present in a newly opened tab in internet explorer using VBA?

Here is the code I wrote.
Option Explicit
Public Sub Press_Button()
Dim objIE As SHDocVw.InternetExplorer 'microsoft internet controls (shdocvw.dll)
Dim htmlDoc As MSHTML.HTMLDocument 'Microsoft HTML Object Library
Dim htmlInput As MSHTML.HTMLInputElement
Dim htmlColl As MSHTML.IHTMLElementCollection
Dim the_input_elements As MSHTML.IHTMLElementCollection
Dim input_element As MSHTML.HTMLInputElement
Dim IeDoc As MSHTML.HTMLDocument
Dim IeDoc2 As MSHTML.HTMLDocument
Dim input_element2 As MSHTML.HTMLInputElement
Dim the_input_elements2 As MSHTML.IHTMLElementCollection
Set objIE = New SHDocVw.InternetExplorer
With objIE
.Navigate "https://www.ndexsystems.com/fengine/fullservice/en/kerrfinancialsalogin.go?fromLogoff=true" ' Main page
.Visible = 1
Do While .readyState <> 4: DoEvents: Loop
Application.Wait (Now + TimeValue("0:00:02"))
'PART 1: set user name and password
Set htmlDoc = .document
Set htmlColl = htmlDoc.getElementsByTagName("INPUT")
Do While htmlDoc.readyState <> "complete": DoEvents: Loop
For Each htmlInput In htmlColl
If htmlInput.Name = "textbox_password" Then
htmlInput.Value = "***"
Else
If htmlInput.Name = "textbox_id" Then
htmlInput.Value = "***"
End If
End If
Next htmlInput
'PART 2: click login
Set htmlDoc = .document
Set htmlColl = htmlDoc.getElementsByTagName("input")
Do While htmlDoc.readyState <> "complete": DoEvents: Loop
For Each htmlInput In htmlColl
If Trim(htmlInput.Type) = "submit" Then
htmlInput.Click
Exit For
End If
Next htmlInput
'PART 3: Clicks on portfolio management button
Do While .Busy: DoEvents: Loop
Do Until .readyState = READYSTATE_COMPLETE: DoEvents: Loop
Set IeDoc = .document
Set the_input_elements = IeDoc.getElementsByClassName("big_button")
For Each input_element In the_input_elements
If input_element.href = "javascript:changePageToFrontdoor(false);" Then
input_element.Click
Exit For
End If
Next input_element
'PART 4: Clicks on the 'Advanced search' button
Do While .Busy: DoEvents: Loop
Do Until .readyState = READYSTATE_COMPLETE: DoEvents: Loop
Set IeDoc2 = .document
Set the_input_elements2 = IeDoc2.getElementsByClassName("parent-item")
For Each input_element2 In the_input_elements2
If input_element2.href = "javascript:directToSearch()" Then
input_element2.Click
Exit For
End If
Next input_element2
End With
End Sub
Parts 1, 2 and 3 work perfectly. When I run this macro, it is actually logging in the website with my credentials. In part 3, it is also clicking on the button called "Porfolio management".
However, by clicking on the "portfolio management" button, a new tab is opened with another page of the same website.
On this newly opened page, there is a button called "Advanced search" that I want to click. Here is the HTML code of the button.
Part 4 is not working with this code. It is not giving me any error, it is just not doing anything. I don't know where my error is because I wrote part 4 with the exact same syntax as part 3 and it is only part 3 that is actually running and giving the correct result (clicking on the button).
Maybe the fact that part 3 opens a new tab of this website should imply an additional step that I didn't do in step 4? Since I am not working with the same tab anymore...
Can anyone help me with finding the error?
Thank you :)
If the URL is different every time and you are not available with that URL than you can try to refer steps below.
(1) create object of Shell application.
(2) Than try to count IE windows and loop through it.
(3) After that match the page title with all the tabs and find the desired tab and assigned it to IE object and than you can try to interact with that page.
Example:
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
On Error Resume Next ' sometimes more web pages are counted than are open
my_url = objShell.Windows(x).Document.Location
my_title = objShell.Windows(x).Document.Title
'find the desired page
If my_title Like "Put something from your IE page title here" & "*" Then
Set ie = objShell.Windows(x)
Exit For
Else
End If
Next
You can modify the above code based on your requirement.
For more information, you can refer link below.
VBA-Excel: Get ALL The Opened Internet Explorer (IE) using Microsoft Excel

VBA Excel Click on a Label

I am new to Excel VBA. I am currently doing excel IE Automation where I want to click label on webpage.
The HTML source code is:
<td class = t19TabItem>Compliance</td>
Any help to click on label compliance.
I used the below code, has found the label compliance by using inner html, but it does not click on compliance tab. It throws errors as object does not this property.
set link=IE.document.getElementsByTagName("a")
if link.InnerHTML="compliance" and link.href="javascript:apex.submit('D_Price')" then
click link
end if
How to click on compliance label?
try the code with references below
Microsoft HTML Object Library
Microsoft Internet Controls**
Sub test()
Dim oHTML_Element As IHTMLElement
Dim oBrowser As InternetExplorer
Dim ie As Variant
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "your web link" 'Your weblink goes here
While ie.readyState <> READYSTATE_COMPLETE And ie.readyState <> READYSTATE_LOADED
DoEvents
Wend
Application.Wait (Now() + TimeValue("00:00:03"))
For Each oHTML_Element In ie.document.getElementsByTagName("a")
If oHTML_Element.innerHTML = "compliance" Then
oHTML_Element.Click
End If
Next
End Sub

excel vba IE clicking a button

Through Excel and VBA, i am opening a page.
I want to click on the button 'Expand' through vba but I am getting an error.
I tried using both VBA commands listed below
Doc.getElementsByClassName("dhl-btn-main collapse-btn-expand-all")(0).Click
Doc.getElementsByClassName("dhl-btn-main collapse-btn-expand-all").Click
The error that I get is
Run time error '438': Object doesn't support this property or method.
Assuming that "Doc" is your reference to IE try this:
Set ElementCol = Doc.document.getElementsByClassName("dhl-btn-main collapse-btn-expand-all")
For Each btnInput In ElementCol
btnInput.Click
Next btnInput
Working Example:
Private Sub IE_Expand()
Dim i As Long
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
'IE.Visible = False
IE.Navigate "https://dhli.dhl.com/dhli-client/shipmentair;jsessionid=q3tzTzyLcL7JkxkNQ4nv7Jtrpzk1glylCyJ7vJzT27h2xBG5zXSm!599496067?0&shipmentId=151218573&accountGroup"
' Wait while IE loading...
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
IE.Visible = True
Set ElementCol = IE.document.getElementsByClassName("dhl-btn-main collapse-btn-expand-all")
ElementCol.Item(0).Click
' Clean up
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing
Application.StatusBar = ""
End Sub

Resources