I have this situation that I'm struggling to solve without any luck. I use VBA in Excel to navigate to a website. On this website, I use VBA to click on a div with id id-01, which works fine. Clicking on that div#id-01 will generate another div with id id-02, I want to click on this div. How can I click on the div with id-02 with VBA?
Set IE = CreateObject("InternetExplorer.Application")
'
'code to navigate to site and wait for site to load
'
'Get div#id-01 and click on it
Set first = IE.document.getElementById("id-01")
'click it
first.click
'The following code does not work because
Set second = IE.document.getElementById("id-02")
second.click
I generally reset the document after the new element has been added by the first.
Try this:
Set IE = CreateObject("InternetExplorer.Application")
'
'code to navigate to site and wait for site to load
'
set myDoc = IE.Document
Set first = myDoc.getElementById("id-01")
'click it
first.click
(WAIT 2 seconds)
set myDoc = IE.Document
Set second = IE.document.getElementById("id-02")
second.click[/code]
Related
I'm trying to open a website, login, select report, and save in Excel format.
I managed to open IE, navigate to the site, enter Username/Password and login.
How do I select the webpage button to open the report page?
Below is the portion of the button on the website I'm attempting to click and code I've tried, both do not give any error message they simply don't do anything.
Website HTML
Sub vbamacro()
Dim IE As Object
Dim IEPage As Object
Dim IEPageElement As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
'Enter website name below
IE.Navigate "websitename"
'Ready state means wait until the website has loaded
Do Until IE.ReadyState = 4
DoEvents
Loop
Set IEPage = IE.Document
Set IEPageElement = IEPage.getElementById("ctl00__contentPlaceHolder__txtUserID")
IEPageElement.Value = "username"
Set IEPageElement = IEPage.getElementById("ctl00__contentPlaceHolder__txtPassword")
IEPageElement.Value = "password"
Set IEPageElement = IEPage.getElementById("ctl00__contentPlaceHolder__btnLogin")
IEPageElement.Click
Do Until IE.ReadyState = 4
DoEvents
Loop
With IE.Document
Set elems = .getElementsByTagName("Li")
For Each e In elems
If (e.getAttribute("ID") = "Reports3") Then
e.Click
Exit For
End If
Next e
End With
End Sub
Also tried;
For each e in ie.document.getElementsByTagName("Li")
If e.ID = "Reports3" Then
e.Click
Exit For
End If
Next
Try navigating directly to the href of the desired element
IE.navigate(e.href)
Also you are clicking “Li” tag element, you have to get the Li children’s tag witch is “a” (the one that has the href property)
I am trying to submit form details. I am unable to share HTML details so tried to explain below.
I have a menu control page https://www.abcmenu.aspx.
This page calls the url https://www.abc-employee.aspx using javascript:void(0), which bring a form in same page when I click on employee menu item.
However, the page does not refresh nor does another page load, and the URL in the address bar remains unchanged.
Here is a sample view of the website:
I need to fill the form details and hit submit button.
The below code gives run time error stating object required.
Set htmldoc = ie.document
Dim emp as mshtml.ihtmlinputelement
Set emp = htmldoc.getelementbyid("fld_emp")
emp.value = 357690
Dim subm as mshtml.ihtmlelememt
Set subm = htmldoc.getelementbyid("btnk_sub")
Subm.click
I tried to debug.print all elements under form tag, but it does not return the elements in the form.
When I execute the code, it returns only the main menu page details and not form elements.
Here is the code I tried to print HTML elements
Dim htmla as mshtml.ihtmlelement
Dim htmlas as mshtml.ihtmlelementcollection
For each htmla in htmlas
Debug.print htmla.innertext
Next htmla
Why am I not able to access HTML elements inside form that was opened in the main menu page?
If you are trying to access iframe elements on the ASP.Net web page using VBA then you can refer to the example below may help you to solve your issue.
Sub demo()
Dim URL As String
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
URL = "https://example.com"
IE.navigate URL
Do While IE.readyState = 4: DoEvents: Loop
Do Until IE.readyState = 4: DoEvents: Loop
Dim elemCollection As IHTMLElementCollection
Debug.Print (IE.document.getElementsByTagName("iframe")(0).contentDocument.getElementsByName("fname")(0).Value)
Set IE = Nothing
End Sub
Output:
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 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 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