I am not new to VBA but I am new to coding anything that interacts with the web. I can open the web page which then has several icons. I need help on clicking a specific icon.
Here is the HTML that appears when I right click the desired icon and click "Inspect element":
<div title="" class="myapps-myapp resource" position="N" index="22">
<div class="myapps-icon-background"></div>
<a class="myapps-icon" href="#">
<img class="iconImage" alt="SomeName"
src="Resources/Icon/aklhdjQ2QWJGVVQxcHpUcEJ5RG5FcEZwcytzPQ--
?size=48"
iconid="akltdjStWJGVVRxcHpUcEJ7QG3FcEZwtytzMT">
</a>
<div class="myapps-status"></div>
<div class="myapps-name">SomeName</div>
</div>
Here is my VBA code that I have so far:
Sub test()
Dim oHTML_Element As IHTMLElement
Dim oBrowser As Internet Explorer
Dim objIE As Variant
Set objIE = Create Object("InternetExplorer.Application")
objIE.navigate "http://The webpage goes here" 'This open the site
While objIE.readyState <> READYSTATE_COMPLETE And objIE.readyState & _
<> READYSTATE_LOADED
DoEvents
Wend
For Each oHTML_Element In objIE.document.getElementsByName & _
("SomeName")
oHTML_Element.Click 'This does not work!
Next
End Sub
Any help would be greatly appreciated. Thanks!
It is the value of the alt attribute you want to match on. No loop.
objIe.document.querySelector("[alt='RevenueCycle P0657 YAVA_AZ']").click
Though it may actually be the a tag you want to click
objIe.document.querySelector("[index='22'] .myapps-icon").click
Read about:
css selectors
querySelector
Related
I am VERY new to VBA and have been watching a ridiculous amount of youtube videos (specifically wise owl tutorials) and have searched here to try and find the answer to my question. I am writing a code to automate part of my job. I have the code to log in to a site that I can't share because I work with classified docs but I will share what I can. Here is the code so far...
Sub Login_and_click_link()
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLDiv As MSHTML.IHTMLElement
Dim HTMLAs As MSHTML.IHTMLElementCollection
Dim HTMLA As MSHTML.IHTMLAnchorElement
'Open Internet Explorer and Navigate to PDDA
With IE
.Visible = True
.Navigate "http://.TheWebsiteINeed.com"
End With
'Loop until PDDA page has fully loaded
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
'Search DOM for element by ID and input username
Set HTMLDoc = IE.Document
Set HTMLInput = HTMLDoc.getElementById("userName")
HTMLInput.Value = Worksheets("sheet1").Range("letternumber").Text
'Search DOM for an element by ID and input password
Set HTMLInput = HTMLDoc.getElementById("Password")
HTMLInput.Value = Worksheets("sheet1").Range("letternumber").Text
'Search DOM for element by ID and click submit
Set HTMLInput = HTMLDoc.getElementById("submit")
HTMLInput.Click
'Navigate to the tab I need
??????
End Sub
There are two links on the page, each will take me to the same page that I need next. So whichever is easier to write code for you can use.
The link at the top is this(within a tr tag):
<span> class="topNavoff" style="cursor: hand;" onmouseover="window.status='Process
Management';this.className='topNavon';"
onmouseout="window.status='';this.className='topNavoff';"
onclick="setTask('pmMain');">Process</span>
The link at the bottom is this (within a p tag):
<span> class="contentSubtitleLink" id="process" style="cursor: hand;"
onmouseover="window.status='Process Management';" onmouseout="window.status='';"
onclick="setTask('pmMain');">Process</span>
Let me know if there is anything I can send to help make this more clear. ANY help would be GREATLY appreciated!
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 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
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 read so many answers to my problem but somehow if I try to "mimic" what I see, I still am not able to do what I need.
The problem is very simple: fill an inputbox on an opened IE page.
Result: the code gets stuck on the line with getelementbyid showing runtime error 424 (object required).
Private Sub AddInfoFromIntranet()
Dim ie As Object
Set ie = CreateObject("internetexplorer.application")
Application.SendKeys "{ESC}" ' I need this to ignore a prompt
With ie
.Visible = True
.navigate "{here goes the address of my website}"
Do Until Not .Busy And .readyState = 4
DoEvents
Loop
.document.getelementbyid("Nachnamevalue").Value = "{here goes whar I want to insert}"
End With
Set ie = Nothing
End Sub
Internet Explorer libraries were naturally imported (otherwise the "internetexplorer.application" wouldn't work.
I am positive that the field I want to fill is called "Nachnamevalue" as from what I learned this morning taking a look around the internet.
The html code of my webpage (only the interesting piece) looks like this:
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
'{here there are info on the style, which i'm gonna ignore}
</style>
</head>
<body bgcolor="#ffffcc"><table width="1000"><tbody><tr><td>
<form name="Suchform" action="index.cfm" method="get" target="bottom_window">
Nachname:
<select name="Nachnamepulldown" class="font09px" onchange="wait_and_search()">
<option value="BEGINS_WITH">beginnt mit
<option value="EQUAL">ist
<option value="CONTAINS">enthält
</option></select>
<input name="Nachnamevalue" onkeyup="wait_and_search()" type="text" size="8">
Abteilung:
<select name="Abteilungpulldown" class="font09px" onchange="wait_and_search()">
<option value="BEGINS_WITH">beginnt mit
<option value="EQUAL">ist
<option value="CONTAINS">enthält
</option></select>
<input name="Abteilungvalue" onkeyup="wait_and_search()" type="text" size="3">
<input name="fuseaction" type="hidden" value="StdSearchResult">
<input type="submit" value="suchen">
<script language="JavaScript" type="text/JavaScript">
document.Suchform.Nachnamevalue.focus();
</script>
</form>
</td></tr></tbody></table></body>
</html>
There is also (I don't know if it can help) an "embedded" javascript that brings results of a search up every time at least 2 characters in the "Nachnamevalue" inputbox are written.
What am I doing wrong?
EDIT:
When I try to execute the Sub step-by-step, I get the following:
Set Doc = ie.document
? Doc
[object HTMLDocument]
( in the watchlist it is an object without any variables inside )
GetElementById gets an element by its id attribute, but "Nachnamevalue" is the value of the name attribute.
To use the name:
.document.Forms("Suchform").Elements("Nachnamevalue").value = "xxx"
This worked for me. The code uses HTML from your question in file c:\Temp\page1.html.
Option Explicit
' Add reference to Microsoft Internet Controls
' Add reference to Microsoft HTML Object Library
Sub AddInfoFromIntranet()
Dim ie As SHDocVw.InternetExplorer
Dim doc As MSHTML.HTMLDocument
Dim elements As MSHTML.IHTMLElementCollection
Dim nachnameValueInput As MSHTML.HTMLInputElement
Set ie = New SHDocVw.InternetExplorer
With ie
.Visible = True
.navigate "c:\Temp\page1.html"
Do Until Not .Busy And .readyState = 4
DoEvents
Loop
Set doc = .document
Set elements = doc.getElementsByName("Nachnamevalue")
If Not elements Is Nothing Then
Set nachnameValueInput = elements(0)
If Not nachnameValueInput Is Nothing Then _
nachnameValueInput.Value = "{here goes whar I want to insert}"
End If
.Quit
End With
Set ie = Nothing
End Sub
To check the names of all input elements which exist at the momonet you execute the VBA code on the page you could use getElementsByTagName("input").
Set elements = doc.getElementsByTagName("input")
If Not elements Is Nothing Then
Dim inputElement
For Each inputElement In elements
Debug.Print inputElement.Name
Next inputElement
End If
You can try cycling all input and selecting the one is named as you need:
Set Elements = IE.document.getelementsbytagname("Input")
For Each Element In Elements
If Element.Name = "Nachnamevalue" Then
Element.Value = {Here your value}
Exit For
End If
Next Element