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
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.
I am trying to use VBA to access data off the web from excel. When I try to go to the website it is redirected to another website which is a banner warning and there is a big button that says "OK" that I need VBA to automatically press. There is a pause in the code using msgbox because the user needs to enter security card clearance which then automatically takes you to the site and then the code starts again when you click the OK on the msgbox as if it is starting at the redirected website (banner warning site).
This is my code for it. I can pull up everything until after the msgbox function and everything after that is not working. any help would be fantastic
Sub GetTable()
Dim ieApp As InternetExplorer
Dim ieDoc As Object
Dim ieTable As Object
Dim clip As DataObject
Set ieApp = New InternetExplorer
ieApp.Visible = True
ieApp.Navigate "***Website URL***"
MsgBox "Click OK After you fill out Card Security Certificate!"
Set ieDoc = ieApp.Document
Set elements = ieDoc.getElementsByTagName("input")
For Each element In elements
If element.Name = "OK" Then
element.Click
End If
Next element
Set elements = Nothing
So I figured out the problem was because I was not using internetexplorermedium. I then just used ready state instead of using the message box as a pause button which made the program much more efficient. Thanks for all the help
I'm currently trying to automate my IE at work with VBA and Excel, because we have some repetitive tasks to complete in an SAP System which is called within the Internet Explorer.
So far I am able to start IE and navigate to the starting page of our System.
I looked on the source code from the Sap System to search for an id from a div (lets say its 'SampleID'). But if I try to click it via VBA I only get returned 'nothing'. So I tried to print out the source code via VBA Debug.Print to look at the problem. And here's the thing:
The source code that gets printed is completely different from the Website's Source Code. In the printed code for example is a iframe that I can't find in the website source code. So that's the reason I can't find my div and any other objects, because they are not in the code that VBA is searching through.
Can anyone tell me what's going wrong? Why are the source codes completely different?
Sub stgGo(){
Dim i As Long
Dim IE As InternetExplorerMedium
Dim objElement As Object
Dim objCollection As Object
Set IE = New InternetExplorer
IE.Visible = True
IE.Navigate ("URL")
'waiting 10 seconds to be really sure javascript is ready
Application.Wait (Now + TimeValue("0:00:10"))
'printing out the false source code
Debug.Print strHTML = myIE.Document.body.innerHTML
Set objCollection = IE.document.getElementsByTagName("div")
i = 0
While i < objCollection.Length
If objCollection(i).ID = "SampleID" Then
objCollection(i).Click
End If
i = i + 1
Wend
End Sub
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
I have a MsgBox that displays both "OK" and "Help" buttons. I would like to redirect to a website when Help button is clicked, but I'm completely unable to achieve it.
I have read in some forums that the easiest way to get it is to make your own userform and to assign a subroutine to each button attached.
Another possible way that I've found is to create a *.hlp file, call it from the Help File parameter and place the proper context ID, but I need to store the help file in a website, and, anyway, I can't find the context IDs numbers.
Thanks in advance for your help!
Here is what you would need to put in the VBE for your help button.
Private Sub HelpButton_Click()
Dim IE as Object
Set IE = CreateObject(InternetExplorer.Application)
IE.Visible = True
IE.Navigate = "Insert your URL here"
End Sub
Here you can find all you need to manage URLs to be opened with Internet Explorer (easiest way) .
In the link I gave it will be shown how to use IE (Internet Explorer) libraries in order to manage some tasks such as opening URL (which you can of course trigger by a click on the "help" button in your userform), filling forms and/or click buttons in a webpage.
Although I think if you have directly a help web page, the first option will suffice.
An overview of what can help you in the link I gave you:
Sub GoToWebSite()
Dim appIE As Object ' InternetExplorer
Dim sURL As String
Application.ScreenUpdating = False
Set appIE = GetIE
sURL = "yourhelpwebpage"
' Here you can add the "on Help button click" action to trigger the following
With appIE
.Navigate sURL
.Visible = True
End With
Application.ScreenUpdating = True
Set appIE = Nothing
End Sub