Web automation by VBA error - cannot click the button (javascript) - vba

all.
I would like to click the button named id='#ICSearch'.Runtime error 424, object required. Since it is an on-click button that loads js. Will that be a problem?
I tried different codes but cannot get it. I am new to this please bear with me.
Private Sub IE_Autiomation()
Dim i As Long
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
'1.
IE.document.getElementById("ICSearch").Click
'2.
IE.document.getElementsByClassName("PSPUSHBUTTONTBADD")(0).Click
End Sub
<DIV class='' id='win0divSEARCHBELOW'><br/><br /><a class='PSPUSHBUTTON'
id = 'Left' role='presentation'><span style='background-Color: transparent;border:0;'>
<input type='button'
id='#ICSearch' name='#ICSearch' class='PSPUSHBUTTONTBADD'
value='Add' onclick="javascript:submitAction_win0(document.win0,
'#ICSearch');" tabindex='18' alt='Add (Alt+1)' title='Add (Alt+1)'/>
</span></a>
</DIV>

This is probably the smallest example of clicking of a button in InternetExplorer with VBA:
Option Explicit
Public Sub TestMe()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "https://github.com"
While IE.Busy
DoEvents
Wend
Dim objLoginLink As Object
Set objLoginLink = IE.Document.getElementsByClassName("text-bold text-white no-underline")
objLoginLink(0).Click
End Sub
This is how objLoginLink looks in the observer window:
The trick is that IE.Document.getElementsBySomething returns a collection, thus you can either loop through it or try to click on the first one.
Use Excel VBA to click on a button in Internet Explorer, when the button has no "name" associated

Related

Call onclick JS event on website button using VBA

I am using VBA to interact with a website in IE.
As part of this I need to click a button there but a simple .Click does not work as I need to trigger the JS onclick event behind this button.
Can someone here tell me how I can achieve this, either by simulating the click and triggering the event or by calling the JS event directly ?
This should happen after the below VBA code.
My VBA code:
Dim IE As Object
Dim MyURL As String
Dim varResults As New DataObject
varResults.SetText TxtSql.Text
varResults.PutInClipboard
Unload FrmResults
Set IE = CreateObject("InternetExplorer.Application")
MyURL = "https://myFirstUrl"
IE.Navigate MyURL
IE.Visible = True
While IE.busy
DoEvents
Wend
Application.Wait (Now + TimeValue("0:00:02")) ' to allow time for loading the page
The onclick JS event behind the button:
javascript:document.forms.jobProfileForm.action='https://mySecondUrl';document.forms.jobProfileForm.submit()
The button's HTML:
<input class="normalbttn" id="editProfileBttn" value="Edit Profile" onclick="theAboveJS" type="button"></input>
Try this:
IE.Document.getelementbyid("editProfileBttn").fireevent ("onclick")

Using VBA to click on a link/button in an IE web page

I have read a lot of information on using VBA to click on a link in IE, but I cannot get it to work in my case.
The relevant HTML code is as follows:
<div id="LinkButton_3" widgetId="LinkButton_3">
<a class="Row_Icon" data-dojo-attach-point="linkNode" data-dojo-attach-event="onClick:_onLinkButtonClicked">Company XYZ. - #12345</a>
</div>
The VBA code I have tried, with 3 different attempts noted, is as follows:
Dim ieApp As SHDocVw.InternetExplorer
Dim ieDoc As MSHTML.HTMLDocument
Dim button As HTMLInputButtonElement
Dim div As HTMLDivElement
' Create a new instance of IE
Set ieApp = New InternetExplorer
' Uncomment this line for debugging purposes.
ieApp.Visible = True
' Go to the page we're interested in.
ieApp.Navigate "MyURL.com"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Set ieDoc = ieApp.Document
' Try #1.
' Nothing happened on the web page after these 3 lines were executed.
Set button = ieDoc.getElementById("LinkButton_3")
button.Focus
button.Click
' Try #2
' Nothing happens - button is not clicked.
Set div = ieDoc.getElementById("LinkButton_3")
div.FireEvent "onClick"
' Try #3
' Nothing happens - button is not clicked.
div.Click
' Close the instance of IE.
ieApp.Quit
' Clean up.
Set ieApp = Nothing
Set ieDoc = Nothing
Any thoughts on what I might be doing wrong or other suggestions would greatly be appreciated.
TMc
You can also use a CSS querySelector of #LinkButton_3 a. This is the a tag within the element with id LinkButton_3. The "#" means id. The " a" means a tag within.
.querySelector method belong to the HTMLDocument.
You can do:
ieDoc.querySelector("#LinkButton_3 a").Click
<div id="LinkButton_3" widgetId="LinkButton_3">
<a class="Row_Icon" data-dojo-attach-point="linkNode" data-dojo-attach-event="onClick:_onLinkButtonClicked">Company XYZ. - #12345</a>
</div>
What you want to click on is the anchor (a) tag, not the div. So, you can find the div with its id and then click on its one and only child with
button.Children(0).Click

VBA Internet Explorer clicking on Text Error

I am new to VBA and it would be great if you can help me on resolving this issue.
I am trying to click on a Text on an IE SharePoint webpage. I am am able to navigate to IE browser, but I am getting a VBA error for clicking the text "Americas" highlighted in Yellow in attached Screenshot. I need help with the IE.Document part of the code at the end of VBA code below. I assume GetElementbyID and GetElementByTagName are correct from HTML code below.
Error - Method Document of Object "IEwebBrowser"Failed
VBA Code:
Private Sub UploadFile()
Dim i As Long
Dim IE As Object
Dim Doc As Object
Dim objElement As Object
Dim objCollection As Object
Dim buttonCollection As Object
Dim AllSpanElements
Dim Span
' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
' Send the form data To URL As POST binary request
IE.navigate "URL"
' Wait while IE loading...
While IE.Busy
DoEvents
Wend
' I AM GETTING ERROR HERE
Set AllSpanElements = IE.Document.getElementById("ext-gen1271").getElementsByTagName("div")
AllSpanElements.Click
Set IE = Nothing
Set objElement = Nothing
End Sub
HTML CODE
<table class="x-grid-table x-grid-table-resizer" border="0" cellspacing="0" cellpadding="0" style="width:10000px;"><tbody>
<tr class="x-grid-row x-grid-row-selected x-grid-row-focused" id="ext-gen1271">
<td class="x-grid-cell-treecolumn x-grid-cell x-grid-cell-treecolumn-1030 x-grid-cell-first">
<div class="x-grid-cell-inner " style="text-align: left; ;"><img src="data:image/gif;base64,R0lGODlhAQABAID/AMDA" class="x-tree-elbow-plus x-tree-expander">
<img src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAA" class="x-tree-icon x-tree-icon-parent ">
Americas</div>
</td>
</tr>
</tbody>
</table>
Give this a shot, I cleaned up the code slightly and I'm trying a slightly different approach. Basically I'm iterating over each element on the page, then clicking it when the InnerText has "Americas" contained with in it.
It may not be the InnerText Property you want to check, it might be the Value or Title so you will need to check that.
Here's the code:
Private Sub UploadFile()
Dim IE As Object: Set IE = CreateObject("InternetExplorer.Application")
Dim Elements As Object
Dim Element As Object
With IE
.Visible = True
' Send the form data To URL As POST binary request
.navigate "URL"
' Wait while IE loading...
While .Busy Or .readystate <> 4
Application.Wait (Now() + TimeValue("00:00:01"))
DoEvents
Wend
End With
' I AM GETTING ERROR HERE
Set Elements = IE.Document.getElementsByTagName("*") ' * is all elements
For Each Element In Elements
If InStr(1, Element.innerText, "Americas") > 0 Then ' If the element has the word Americas...click it
Element.Focus
Element.Click
Element.FireEvent ("OnClick")
End If
Next
'Clean up
Set IE = Nothing
End Sub

how to click a button on webpage having class name using vba excel

I am a rookie in VBA excel.
There is a web page application in which
i need to click a button, the source of which is
<em class="x-btn-arow" unselectable="on">
<button class= x-btn-text" id="ext-gen7576" style="" type="button">Actions</button>
Sub xx()
Dim IE As Object
Dim doc As HTMLDocument
Dim l As IHTMLElement
Dim lo As IHTMLElementCollection
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "http://theapplicationlink"
Do
DoEvents
Loop Until IE.ReadyState = 4
Set doc = IE.Document
Set lo = doc.getElementsByTagName("button")
For Each l In lo
If l.getAttribute("class") = "x-btn-text" Then
l.click
End If
Next
End Sub
it doesn't throw any error but it doesn't click the button.
I cannot use ID as it keeps on changing each time i launch the application.
Also the class and type is same for other buttons also.
Forgive me for any technical errors
Any help will be a huge favour here.
There is an id. Does it change completely or does part of it remain the same? If it were you could partial match on the bit that remain the same using a CSS selector.
That aside you could use:
objIE.document.querySelector("button[class*= x-btn-text]").Click
This uses a CSS selector to target the element of button[class*= x-btn-text]. Which will be the first element with button tag having attribute class with value containing x-btn-text.
"button" is not a HTML tag. use a "Tag". let me give you an example here. Replace the "strTagName" with a HTML tage that inlcudes the thing you want to click.
Dim objTag As Object
For Each objTag In objIE.document.getElementsByTagName(strTagName)
If InStr(objTag.outerHTML, "x-btn-text") > 0 Then
objTag.Click
Exit For
End If
Next

IE11 Automation: Change value of drop down list

I have included the code I use to navigate to a particular website; the end game here is to try and click the drop down list under "Login to" and select an option and click go. This is a learning experience for me. I would love to know the WHY? behind the code.
Sub internetautomation2()
Dim IE As InternetExplorer 'Created Class of type InternetExplorer
Set IE = New InternetExplorer 'Initiated Class, object IE created
'Properties and methods of
'Internet Explorer now usable
IE.Top = 0 'IE window properties Topleft corner
IE.width = 1500 '1500pixels wide
IE.Visible = True
IE.Navigate ("ibc.com") 'Navigate to particular website
While IE.ReadyState = READYSTATE_LOADING 'Wait for website to load
DoEvents
Wend
'Clicking drop down list,selecting option,click go
End Sub
Not sure what this question has to do with JavaScript, but just wire up a change event handler to your list and navigate to the list value:
Private Sub ComboBox1_Change()
IE.Navigate (ComboBox1.Value)
End Sub
This should do it:
'Set to the secod option "Deposit express"
IE.Document.getElementById("ctl00_IBCOtherOnlineServices_ddlLoginTo").selectedIndex = 1
'click the submit button
IE.Document.getElementById("ctl00_IBCOtherOnlineServices_btnLoginTo").Click