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

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

Related

How to access HTML elements in ASPX menu page?

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:

VBA Clicking on an Item with Internet Exporer

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.

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

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

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: not able to pull value within <input> tag using getelementsbyTagname().Value

This is my first post on stackflow :) I've been Googling VBA knowledge and writing some VBA for about a month.
My computer info:
1.window 8.1
2.excel 2013
3.ie 11
My excel reference
Microsoft Object Library: yes
Microsoft Internet Controls: yes
Microsoft Form 2.0 Object library: yes
Microsoft Script Control 1.0: yes
Issue:
I was trying to retrieve data from internet explorer automatically using VBA.
I would like to retrieve the value within an input tag from a id called "u_0_1" which is under a id called "facebook". I am expecting to retrieve the value "AQFFmT0qn1TW" on cell c2. However, it got this msg popped up after I run the VBA "run-time error '91':object variable or with block variable not set.
I have been trying this for a couple of weeks using different methods such as,
1.getelementsbyClassname
2.getelementbyid
3.getelementsbyTagname
But it just doesn't work.
url:
http://coursesweb.net/javascript/getelementsbytagname
Below is my VBA code. Could you guys help me out a little bit please?
Private Sub CommandButton1_Click()
Dim ie As Object
Dim Doc As HTMLDocument
Dim getThis As String
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = 0
ie.navigate "http://coursesweb.net/javascript/getelementsbytagname"
Do
DoEvents
Loop Until ie.readyState = 4
Set Doc = ie.document
getThis = Trim(Doc.getElementById("u_0_1")(0).getElementsByTagName("input")(0).Value)
Range("c2").Value = getThis
End Sub
Thanks for your help. I have no idea that there is difference between JS and VBA in aspect of getelementsby () methods. And using the loop method to find the id which I find it very useful as well.
I still have some issues to retrieve value from a form or input type. I hope that you could help me or give me some suggestions as well.
Expected Result:
retrieve the value "AQFFmT0qn1TW" and copy it on Cell ("c2") automatically.
Actual Result:
nothing return to Cell ("C2")
Below is the HTML elements.
<form rel="async" ajaxify="/plugins/like/connect" method="post" action="/plugins/like/connect" onsubmit="return window.Event && Event.__inlineSubmit && Event.__inlineSubmit(this,event)" id="u_0_1">
<input type="hidden" name="fb_dtsg" value="AQFFmT0qn1TW" autocomplete="off">
Below is the VBA code based on your code.
Private Sub CommandButton1_Click()
Dim ie As Object
Dim Doc As HTMLDocument
Dim Elements As IHTMLElementCollection
Dim Element As IHTMLElement
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = 0
ie.navigate "http://coursesweb.net/javascript/getelementsbytagname"
Do
DoEvents
Loop Until ie.readyState = 4
Set Doc = ie.document
Set Elements = Doc.getElementsByTagName("input")
For Each Element In Elements
If Element.name = "fb_dtsg" Then
Range("c2").Value = Element.innerText
End If
Next Element
Set Elements = Nothing
End Sub
Cheers.
first of all, I can't find in source of website tags you were searching. Anyway, I think you can't chain getElementById.getElementsByTag as in JS. You have to loop through collection of document elements.
Private Sub CommandButton1_Click()
Dim ie As Object
Dim Doc As HTMLDocument
Dim Elements As IHTMLElementCollection
Dim Element As IHTMLElement
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = 0
ie.navigate "http://coursesweb.net/javascript/getelementsbytagname"
Do
DoEvents
Loop Until ie.readyState = 4
Set Doc = ie.document
Set Elements = Doc.getElementsByTagName("ul")
For Each Element In Elements
If Element.ID = "ex4" Then
Sheets(1).Cells(1, 1).Value = Element.innerText
End If
Next Element
Set Elements = Nothing
End Sub
First I'm getting collection of tags "ul", then looping through them for id "ex4". In your case you'd get collection of "input"s then loop for id you want. Finding id which is followed by different id shouldn't be hard, just some if...thens.
If you need further assistant please respond with url in which I can find exactly what you're looking for.
Cheers