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")
Related
My company uses a web based reporting system and I'm trying to automatically export the data to Excel. There is a standard dropdown menu with File > Export to XLS and I would like to automatically click the "Export" button to download the report.
I have read through other posts dealing with clicking buttons to expand a page, or enter a value into a form, etc, but possibly because the button is in a drop down menu, or because the element is within an iframe none of the previously posted solutions have worked for me.
So Far:
I can navigate to the page
I can get the menuExportXLS element
Debug.print prints the correct innerText
My code is not throwing any errors
BUT I can't successfully click the button to download the file.
Alternatively, there is a javascript onclick script within the HTML that could trigger the download, but I don't know how to call it in VBA. I would be happy to get either method running.
Sample of HTML
Sub ExportXLSTest()
Dim appIE As InternetExplorerMedium
Dim Obj As Object
Set appIE = New InternetExplorerMedium
sURL = "https://Site"
With appIE
.Navigate sURL
.Visible = True
End With
Do While appIE.Busy Or appIE.ReadyState <> 4
DoEvents
Loop
Application.Wait (Now + TimeValue("0:00:05"))
Set Obj = appIE.document.getElementsByTagName("iframe")(0).contentDocument.getElementById("menuExportXLS")
Debug.Print Obj.innerText
Obj.Click
Set appIE = Nothing
End Sub
You can use a CSS selector to target the element;
appIE.document.getElementsByTagName("iframe")(0).contentDocument.querySelector("[onclick*='exportXLS']").Click
This is an attribute = value selector that targets the first element in the content document that has an onclick attribute containing the value exportXLS.
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
I know there is a similar question to this - but it doesn't show enough detail to help me, I am still new to this and need a little more guidance. What I am trying to do is use a VBA Macro to press a button on a web page.
The details that I can find out about the button are
class = "urBtnEmph"
id = "b1_pki"
ct= "Button"
onclick = "LoginViaPKI();"
It isn't a page I have written, or have access to change anything on. But is there anyway I can automate clicking the button?
Thank You
Try this with IE:
Dim IE As New InternetExplorer
Dim doc As HTMLDocument
Set doc = IE.document
'below line may be needed in case the page load is slow
'Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
Dim Button As Object
Set Button = doc.getElementById("b1_pki")
Button.Click
It requires reference to Microsoft Internet Controls and Microsoft HTML Object Library
I'm using the following code to navigate to a web site and then predetermine the drop downs on the page and it works fine...
Private Sub CommandButton4_Click()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "https://postimage.io/"
IE.Visible = True
While IE.busy
DoEvents
Wend
IE.document.All("optsize").Value = "8"
IE.document.All("expire").Value = "0"
End Sub
... however there is a button on the page Choose images i would like my script to click that and in the pop-up enter a address of an image then press open
is this possible? if so could someone please help me achieve it
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