PDA

View Full Version : Excel Automation of IE - help click this button



rob88
02-24-2013, 01:38 PM
Hi guys, i have been stuck on this problem for a while now and i really need some help :banghead:. I am new to VB so there is no doubt i have overlooked something quite simple.

I need to click a button on a website, below is the source code ( i have also attached this as a word document as it may be easier to see)


<head id="ctl00_Head1"><title> Enquiry Menu</title><link id="ctl00_StyleSheetLink" href="../StyleSheet/PdaStyleSheet.css" type="text/css" rel="stylesheet" /> <script language="javascript" src="../Script/Script.js"></script> <script language="javascript" src="../Script/SignaturePadScript.js"></script> <script language="javascript" src="Script/Script.js"></script> <script language="javascript" src="Script/SignaturePadScript.js"></script> <!-- need to put this reference in as it looks for scripts relative to the parent page, not the master page --> <!-- <script language="javascript" src="Script/Script.js"></script> <script language="javascript" src="Script/SignaturePadScript.js"></script> --> <script language="javascript"> var lastControl = null; /*function MasterPage_ShowErrorMessage(obj, errorHeading, errorText) { var popUpObject = document.getElementById("PopupDialogBox"); var imageObj = obj; if (imageObj != lastControl) { lastControl = imageObj; //get the position of the clicked object var coors = findPos(obj); //then add 15 px to each one so we don't cover up the exclamation mark coors[1] += 15; coors[0] += 15; popUpObject.style.top = coors[1] + 'px'; popUpObject.style.left = coors[0] + 'px'; popUpObject.style.width = 200; if (popUpObject.style.pixelLeft + popUpObject.style.pixelWidth > document.body.clientWidth) popUpObject.style.pixelLeft = event.clientX - 10 - popUpObject.style.pixelWidth; popUpObject.style.display = "block"; document.getElementById("PopupDialogBoxHeading").innerText = errorHeading; document.getElementById("PopupDialogBoxMessage").innerText = errorText; } else { lastControl = null; popUpObject.style.display = "none"; } event.cancelBubble = true; } //as the position in at which the object we're trying to render will be //relative to any parent it has with absolute positioning, we want to stop //adding to the co-ordinates at which to render the object as soon as we //hit a parent with an absolute position. function findPos(obj) { var curleft = curtop = 0; if (obj.offsetParent) { curleft = obj.offsetLeft; curtop = obj.offsetTop; while ((obj = obj.offsetParent) && (getElementStyle(obj, 'position', 'position') != 'absolute')) { curleft += obj.offsetLeft; curtop += obj.offsetTop; } } return [curleft,curtop]; }*/ //expects an element and the text representations of the style property //we care about (use both IE and CCS names so it will work if someone //uses a browser other than IE). function getElementStyle(elem, IEStyleProp, CSSStyleProp) { if (elem.currentStyle) { return elem.currentStyle[IEStyleProp]; } else if (window.getComputedStyle) { var compStyle = window.getComputedStyle(elem, ""); return compStyle.getPropertyValue(CSSStyleProp); } return ""; } function MasterPage_ShowErrorMessage(obj, errorHeading, errorText) { var popUpObject = document.getElementById("PopupDialogBox"); if (obj != lastControl) { lastControl = obj; popUpObject.style.left = event.clientX + 10; popUpObject.style.top = event.clientY + 10; popUpObject.style.width = 200; if (popUpObject.style.pixelLeft + popUpObject.style.pixelWidth > document.body.clientWidth) popUpObject.style.pixelLeft = event.clientX - 10 - popUpObject.style.pixelWidth; popUpObject.style.display = "block"; document.getElementById("PopupDialogBoxHeading").innerText = errorHeading; document.getElementById("PopupDialogBoxMessage").innerText = errorText; } else { lastControl = null; popUpObject.style.display = "none"; } event.cancelBubble = true; } function DropDownChange(selectedIndex) { document.getElementById("ctl00$DropDownListSelectedIndex").value = selectedIndex; //document.getElementById("ctl00$ctl19").disabled=true; } function TextBoxChange(value) { document.getElementById("ctl00$MessageDialogTextBoxText").value = value; } function CheckBoxChange(checked) { document.getElementById("ctl00$CheckBoxCheckedTextBox").value = checked; } </script></head><body scroll="no" onclick="CatchClick(window.event.srcElement); HidePopupIfVisible();" onload="getSignature('ctl00_PageAreaContentPlaceHolder_'); SetDefaultButton(); if (window.document.title!='Home'){SetFocus();}; AutoRefresh();" onresize="ResizeWindow(600, 800);" onkeydown="CatchKeyPress(window.event.keyCode, window.event.srcElement);"> <form name="aspnetForm" method="post" action="AccountingEnquiryMenu.aspx?RoleID=206" id="aspnetForm"><div><input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" /><input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" /><input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTY0MzEyMDIwOA9kFgJmD2QWAgIDD2QWBAIBDxYCHglpbm5lcmh0bWwFIlBEJmFtcDtB IEFjY291bnRpbmcgLSBFbnF1aXJ5IE1lbnVkAgMPFgQeA3NyYwUVLi4vSW1hZ2UvbmJzbG9nbzM uZ2lmHgV0aXRsZQU+UHJvZHVjdGlvbiBFbnZpcm9ubWVudA0KQ1NQUFVJUzAyDQpCdWlsZDogNC 4wLjAuMCAoNC4wLjMzNjkuMSlkZEJJDGxYtPGALzuh0onm4Kxui0c4" /></div> <script type="text/javascript"> //<![CDATA[var theForm = document.forms['aspnetForm'];if (!theForm) { theForm = document.aspnetForm;}function __doPostBack(eventTarget, eventArgument) { if (!theForm.onsubmit || (theForm.onsubmit() != false)) { theForm.__EVENTTARGET.value = eventTarget; theForm.__EVENTARGUMENT.value = eventArgument; theForm.submit(); }}//]]></script> <div> <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWDALPj8D6CgKq7qvMCwLKuvL/DALwnLmMAwKwxYanBQLp6ubsBgKV7M2LCwLG1/yvDwKskqPbAQLC5OTqAwKgtMnpCwLStb6OCIGym7YlFfm4ZkOW4SRNSaIkPJcT" /></div> <div class="WrenMenuContainer"> <h1 id="ctl00_WrenMenuHeader">PD&amp;A Accounting - Enquiry Menu</h1> <img src="../Image/nbslogo3.gif" id="ctl00_WrenMenuLogoImage" border="0" align="absMiddle" title="Production EnvironmentCSPPUIS02Build: 4.0.0.0 (4.0.3369.1)" /> <div> <ul id="ctl00_WrenMenuLinkButtonList"><li><a title="Exit the application" href="javascript:__doPostBack('ctl00$ctl01','')" style="display:inline-block;width:80px;margin-left:5px;">Exit</a></li></ul> <span id="ctl00_WrenMenuUserIdLabel"></span> </div> <div class="WrenMenuFadeStrip"></div> </div> <div class="UserMessageContainer"><span id="ctl00_MessageLabel">&nbsp;</span></div> <!--<div style="padding:5px 10px 5px 10px; color:Firebrick; font-size:8pt;"><u>Summary</u> > <u>Accounting</u> > <u>Search</u> > <b>Rule Detail</b></div>--> <div style="text-align:center;"> <h3>Accounting Enquiry Main Menu</h3> <input type="submit" name="ctl00$PageAreaContentPlaceholder$ButtonNavigateToPaymentEnquiry" value="Payment Enquiry" id="ctl00_PageAreaContentPlaceholder_ButtonNavigateToPaymentEnquiry" class="Buttons" style="font-weight:bold;height:25px;width:280px;" /> <br /><br /> <input type="submit" name="ctl00$PageAreaContentPlaceholder$ButtonNavigateToExtractEnquiry" value="Extract Enquiry" id="ctl00_PageAreaContentPlaceholder_ButtonNavigateToExtractEnquiry" class="Buttons" style="font-weight:bold;height:25px;width:280px;" /> <br /><br /> <input type="submit" name="ctl00$PageAreaContentPlaceholder$ButtonNavigateToTransitEnquiry" value="Trial Balance" id="ctl00_PageAreaContentPlaceholder_ButtonNavigateToTransitEnquiry" class="Buttons" style="font-weight:bold;height:25px;width:280px;" /> <br /><br /> <input type="submit" name="ctl00$PageAreaContentPlaceholder$ButtonNavigateToSummaryEnquiry" value="Summary Enquiry" id="ctl00_PageAreaContentPlaceholder_ButtonNavigateToSummaryEnquiry" class="Buttons" style="font-weight:bold;height:25px;width:280px;" /> <br /><br /> <input type="submit" name="ctl00$PageAreaContentPlaceholder$ButtonNavigateToAccountingEnquiry" value="Accounting Enquiry" id="ctl00_PageAreaContentPlaceholder_ButtonNavigateToAccountingEnquiry" class="Buttons" style="font-weight:bold;height:25px;width:280px;" /> <br /><br /> <input type="submit" name="ctl00$PageAreaContentPlaceholder$ButtonNavigateToRuleEnquiry" value="Rule Enquiry" id="ctl00_PageAreaContentPlaceholder_ButtonNavigateToRuleEnquiry" class="Buttons" style="font-weight:bold;height:25px;width:280px;" /> <br /><br /> </div> <div id="ctl00_ButtonContainer" class="ButtonContainer"></div> <div id="PopupDialogBox" class="PopupDialogBox"> <h1 id="PopupDialogBoxHeading">Message Heading</h1> <div id="PopupDialogBoxMessage">Message text of some sort.... dah dah dahhhh...</div> </div> <input name="ctl00$DropDownListSelectedIndex" type="text" value="0" id="ctl00_DropDownListSelectedIndex" class="Invisible" /> <input name="ctl00$MessageDialogTextBoxText" type="text" id="ctl00_MessageDialogTextBoxText" class="Invisible" /> <input name="ctl00$CheckBoxCheckedTextBox" type="text" value="false" id="ctl00_CheckBoxCheckedTextBox" class="Invisible" /> <input name="ctl00$RefreshSeconds" type="text" value="1260000" id="ctl00_RefreshSeconds" class="Invisible" /> </form></body></html>

The button i need to click on has the name attribute value of
ctl00$PageAreaContentPlaceholder$ButtonNavigateToAccountingEnquiry is represented by the section of code below


<input type="submit" name="ctl00$PageAreaContentPlaceholder$ButtonNavigateToAccountingEnquiry" value="Accounting Enquiry" id="ctl00_PageAreaContentPlaceholder_ButtonNavigateToAccountingEnquiry" class="Buttons" style="font-weight:bold;height:25px;width:280px;" />



So far, i have tried the following but nothing happens. The page loads and the button remains unclicked.




FIRSTLY

Set htmlDoc = .document
Set htmlColl = htmlDoc.getElementsByTagName("input")
Do While htmlDoc.readyState <> "complete": DoEvents: Loop
For Each htmlInput In htmlColl
If Trim(htmlInput.getAttribute("class")) = "Buttons" and Trim(htmlInput.getAttribute("name")) = “ctl00$PageAreaContentPlaceholder$ButtonNavigateToAccountingEnquiry” Then

htmlInput.Click
Exit For
End If

Next htmlInput


SECONDLY



Set inp = IE.document.getElementsByTagName("input")

For Each itm In inp

If itm.Type = "submit" And itm.Value = " Accounting Enquiry " Then
itm.Select
itm.Click

End If


Next itm






Any suggestions would be greatley appreciated. I have noticed there is some Java code within the HTML source code but this means absolutely nothing to me.:doh:

mohanvijay
02-25-2013, 12:03 AM
try this


Dim OB_InEx As SHDocVw.InternetExplorer
Dim htmlDoc As MSHTML.HTMLDocument
Set OB_InEx = New SHDocVw.InternetExplorer
OB_InEx.Navigate "your url"
Do While htmlDoc.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
Set htmlDoc = OB_InEx.Document
htmlDoc.all.Item("ctl00_PageAreaContentPlaceholder_ButtonNavigateToAccountingEnquiry").Click

rob88
02-25-2013, 02:41 AM
try this


Dim OB_InEx As SHDocVw.InternetExplorer
Dim htmlDoc As MSHTML.HTMLDocument
Set OB_InEx = New SHDocVw.InternetExplorer
OB_InEx.Navigate "your url"
Do While htmlDoc.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
Set htmlDoc = OB_InEx.Document
htmlDoc.all.Item("ctl00_PageAreaContentPlaceholder_ButtonNavigateToAccountingEnquiry").Click



Thanks for the response, i may be wrong but does the above assume i am starting from the beginning, i.e its creating a new instance of IE?

Prehaps i should have made clear, there are a few steps before i get to the point where i need to click this button.

Initially i load the URL i need with

Dim objIE As SHDocVw.InternetExplorer 'microsoft internet controls (shdocvw.dll)
Dim htmlDoc As MSHTML.HTMLDocument 'Microsoft HTML Object Library
Dim htmlInput As MSHTML.HTMLInputElement
Dim htmlColl As MSHTML.IHTMLElementCollection
Set IE = CreateObject("Internetexplorer.Application")
With IE
.Visible = True
.navigate "MY URL"
.Visible = True
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop


Then i enter the section of the site that i need via the below (clicking this first button seems to work fine :dunno

Dim cb As Object
Set cb = IE.document.GetElementByID("ctl00_PageAreaContentPlaceholder_AgreementCheckbox")
cb.Checked = True
Set inp = IE.document.getElementsByTagName("input")
For Each itm In inp
If itm.Type = "submit" And itm.Value = "Enter" Then
itm.Select
itm.Click
End If

Next itm

Once this button has been clicked IE beins to load the next section of the website (this is the part i now need to click) and the HTML source i have provided is the source generated at this point.

Is it possible for security reasons the creator of the site have somehow blocked automation ?

I just dont understand as i have the ID and name of the button and have used the same concept as the first button that works fine :banghead:

So i need some code that will move on from the code above and not from the start :)

mohanvijay
02-25-2013, 06:29 AM
Dim cb As Object
Set cb = IE.Document.GetElementByID("ctl00_PageAreaContentPlaceholder_AgreementCheckbox")
cb.Checked = True
Set inp = IE.Document.getElementsByTagName("input")
For Each itm In inp
If itm.Type = "submit" And itm.Value = "Enter" Then
itm.Select
itm.Click
Exit For
End If

Next itm
Do While IE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
IE.Document.all.Item("ctl00_PageAreaContentPlaceholder_ButtonNavigateToAccountingEnquiry").Click

rob88
02-25-2013, 07:37 AM
Dim cb As Object
Set cb = IE.Document.GetElementByID("ctl00_PageAreaContentPlaceholder_AgreementCheckbox")
cb.Checked = True
Set inp = IE.Document.getElementsByTagName("input")
For Each itm In inp
If itm.Type = "submit" And itm.Value = "Enter" Then
itm.Select
itm.Click
Exit For
End If

Next itm
Do While IE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
IE.Document.all.Item("ctl00_PageAreaContentPlaceholder_ButtonNavigateToAccountingEnquiry").Click



Thanks for the reply :)

unfortunately i get the following error when i try to run this

"Run-time error '91' Object variable or With block variable not set"

for the following line
IE.document.all.Item("ctl00_PageAreaContentPlaceholder_ButtonNavigateToAccountingEnquiry").Click

rob88
02-25-2013, 10:19 AM
I have acutally achieved what i need by using application.sendkeys method :) It seems to work (for now anyway)

I am on the last step now. I have a tab strip with a number of difference tabs and i need to click on a certain one.

Please see the attached word doc outlining the source code for this tab.

I can see the finish line now, just need a little help to get there


Thanks :bow: