VBA Excel Value not recorded/accepted in IE - excel

I have a problem with IE not accepting a value (the name of a computer) that I am trying to put into a field. I set the value, it becomes visible in the field, but then when I click "Submit" on the form, it (IE) fails to recognize the value I entered. I am able to modify other objects on the page, but not this simple text field. I can even retrieve the Value from the object in IE after setting it. The field is required, so the submit routine fails before I can proceed.
Here's some code:
'Find the correct instance of IE
Set objShell = CreateObject("Shell.Application")
' etc.
Set IE = objShell.Windows(x)
' etc.
' Enter Computer_Name
Set objCollection = IE.Document.getElementsByTagName("iframe")(2).contentDocument.getElementById("QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC")
objCollection.Value = Computer_Name ' (The text I'm trying to enter)
' Some other stuff that is working
' Click the "Submit" button on the form (this works too).
After clicking "Submit", the webpage pops up an error saying that the required field (the computer name) was not entered. The field is highlighted red.
When I enter the computer name manually, it works just fine, but not when using VBA. Any help would be appreciated!
Here is a sample of the HTML code. I'll highlight the element I'm trying to modify.
<div class="questionContainer ">
<div class="left questionBody" id="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC-label-body" required="false">
<label class="questionlabel bold" id="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC-label" for="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC">Computer Name<span class="required">*</span></label>
</div>
<div class="left answerBody block">
<div class="left">
<div id="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC-answer-body">
' *********** The next line is the element in question. ************
<input tabindex="0" class="answerTextarea " id="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC" aria-describedby="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC-instructions" aria-labelledby="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC-label" required="true" type="text" size="40" value="" responsetype="TEXT" questiondefid="QDHAA5V0GH4LSAO2AI6F2MXNIAJ5CE" totalorder="3" questionid="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC" level="0" overwrite="1" maxlng="16" wrap="virtual">
</div>
</div>
<div class="validationResult clear" id="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC-validationResult" style="display: none;"></div>
<div class="clear"></div>
<div class="instructions" id="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC-instructions"></div>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
Possibly important note: the bit of code in one of the later lines of code that says "style="display: none;" does not appear until the value is entered into the filed manually in IE.
Thanks!

Thank you Tim Williams for your help with ideas which put me on the journey that led to this solution. It turned out that I needed to trigger the events for the page to accept the value. Amazingly simple, when I come to realize it. Here is what my final code looks like:
Dim objEvent ' **** NEW ****
'Find the correct instance of IE (opened manually initially)
Set objShell = CreateObject("Shell.Application")
' etc.
Set IE = objShell.Windows(x)
' etc. (including navigation when necessary)
' After the webpage has loaded, set the event handler ' **** NEW ****
Set objEvent = IE.Document.createEvent("HTMLEvents") ' **** NEW ****
' Enter Computer_Name
Set objCollection = IE.Document.getElementsByTagName("iframe")(2).contentDocument.getElementById("QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC")
objCollection.Value = Computer_Name ' (The text I'm entering)
' Trigger the event change ' **** NEW ****
objEvent.initEvent "change", False, True ' **** NEW ****
objCollection.dispatchEvent objEvent ' **** NEW ****
' Enter other needed information
' Click the "Submit" button on the form.

Related

How to click on the button after data paste

I am trying to click on the button after I have pasted a value on a text box. However none of the code I tried seems to work. On the same column there are 2 text boxes and few buttons. I managed to open up a new frame after clicked on the 1st button with the below code:
Set the_button_elements = doc.getElementsByTagName("div")
For Each button_element In the_button_elements
If button_element.getAttribute("class") = "pzbtn-mid" Then
button_element.Click
Exit For
End If
Next button_element
I have tried by changing the tag name and attribution and also the below code but it still doesn't work:
doc.querySelectorAll("[type=button]").item(3).Click
Below is the element for the 1st button which work with the above code:
<button type="button" class="pzhc" id="AcctNumber"
disabled="" onclick="this.disabled=true;javascript: LoadAcct();"
title="Search">
<div class="pzbtn-lft">
<div class="pzbtn-rgt">
<div class="pzbtn-mid" data-click="...">
<img class="pzbtn-i">Go</div></div></div></button>
Below is the element for the next button which I am trying to find the code to make it work:
<button type="button" class="pzhc" id="SearchBtn"
disabled="true"onclick="getCaseDetails();">
<div class="pzbtn-lft">
<div class="pzbtn-rgt">`enter code here`
<div class="pzbtn-mid">
<img class="pzbtn-i">Search</div></div></div></button>
Appreciate someone could guide me by providing me the code that click on the button as the button is still grey out even after the text pasted. However this is the same situation for the 1st code but it still works.
Perhaps the issue relates to the disabled attribute. If we want to click the button, we should make sure it is enabled status. After clicking the first button or in the textbox change event, you could use the following JavaScript code to enable the Search button:
document.getElementById("SearchBtn").disabled = false;
You could check the following sample:
<button type="button" class="pzhc" id="AcctNumber" onclick='this.disabled=true;javascript: alert("Go"); document.getElementById("SearchBtn").disabled = false;'
title="Search">
<div class="pzbtn-lft">
<div class="pzbtn-rgt">
<div class="pzbtn-mid" data-click="...">
<img class="pzbtn-i">Go
</div>
</div>
</div>
</button>
<button type="button" class="pzhc" id="SearchBtn"
disabled="true" onclick='javascript: alert("Search");'>
<div class="pzbtn-lft">
<div class="pzbtn-rgt">
`enter code here`
<div class="pzbtn-mid">
<img class="pzbtn-i">Search
</div>
</div>
</div>
</button>
Then, if I use the following VBA script click the button, it will click the Go button, then, enable the search button and click it:
Sub login()
Const Url$ = "https://dillion132.github.io/vbatestpage.html"
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.navigate Url
ieBusy ie
.Visible = True
'Find the related button
Dim button_go_element As Object, button_search As Object
Set button_go_element = .document.getElementById("AcctNumber")
Set button_search = .document.getElementById("SearchBtn")
button_go_element.Click
button_search.Click
End With
End Sub
Sub ieBusy(ie As Object)
Do While ie.Busy Or ie.readyState < 4
DoEvents
Loop
End Sub

VBA fire event on empty HTML div

I try to automate some manual processes by using VBA automation. One of them is to click on a element that has an empty content and at the moment I am not able to figure out how to deal with it
The HTML code that I am trying to click on:
<div id="searchcombobox-1077-triggerWrap" data-ref="triggerWrap" class="x-form-trigger-wrap x-form-trigger-wrap-toolbar">
<div id="searchcombobox-1077-inputWrap" data-ref="inputWrap" class="x-form-text-wrap x-form-text-wrap-toolbar">
<input id="searchcombobox-1077-inputEl" data-ref="inputEl" type="text" role="combobox" size="1" name="searchcombobox-1077-inputEl" placeholder="Account Number" tabindex="-1" class="x-form-field x-form-text x-form-text-toolbar " autocomplete="off" componentid="searchcombobox-1077">
</div>
<div id="searchcombobox-1077-trigger-picker" class="x-form-trigger x-form-trigger-toolbar x-form-search-trigger x-form-search-trigger-toolbar "></div>
</div>
The VBA code used is:
HTMLDoc.getElementById("searchcombobox-1077-inputEl").Value = '11xx111'
Set click_el = HTMLDoc.querySelector("#searchcombobox-1077-trigger-picker")
With click_el
.Focus
.FireEvent "onclick"
End With
What should be the approach that I need to take into consideration since the div element that I need to click on is empty?
Thanks,

Tick Checkbox IE Automation Excel VBA

I'm trying to export a file from website but for that i have to select the elements i want to export from a checkbox.
The HTML code for the checkbox i want to tick is:
<input id="MainReport_ctl04_ctl09_divDropDown_ctl00" type="checkbox" name="MainReport$ctl04$ctl09$divDropDown$ctl00" onclick="$get('MainReport_ctl04_ctl09').control.OnSelectAllClick(this);" class="">
I've tried:
With IE.document.getElementsByName("MainReport$ctl04$ctl09$divDropDown$ctl00")
.Item.Click
end with
And
With IE.document.getElementsByName("MainReport$ctl04$ctl09$divDropDown$ctl00")
.Item(0).Checked = True
End With
And
Set obj = IE.document.getElementsByName("MainReport$ctl04$ctl09$divDropDown$ctl00")
obj.FireEvent ("onclick")
But nothing seems to work.
Could someone help me please? Thank u in advanced!

Trying to select an item in kendogrid kendoDropDownList using Excel VBA

I'm trying to select an item on a dropdown list on an internal company web page. I am able to set text entry items and get the dropdownlist to open, but I' having a hard time figuring out how to make a selection
In other area I'm able to make a selection using the ID and td/tr tags, but this routine doesn't have any tr/td tags with entry names.
Here'a couple of things I've tried so far:
These two items perform the same function and work fine for opening the first dropdown
ie.Document.getElementById("FromDistrict").Click
ie.Document.parentWindow.execScript "$('#FromDistrict').kendoDropDownList('open');"
I've tried several variations of these:
ie.Document.parentWindow.execScript "$('#FromDistrict').data('kendoGrid').dataItem($('transport').data('kendoDropDownList').select('KILGORE'));"
ie.Document.parentWindow.execScript "$('#FromDistrict').data('kendoGrid').data('kendoDropDownList').select('KILGORE'));"
ie.Document.parentWindow.execScript "$('#FromDistrict').data('kendoGrid').data('kendoDropDownList').select('eq:0'));"
ie.Document.parentWindow.execScript "$('#FromDistrict').data('kendoDropDownList').select('KILGORE'));"
ie.Document.parentWindow.execScript "$('#FromDistrict').select('KILGORE'));"
Viewing the source code for this particular segment is:
</div>
<form action="/TransferLoad/Add" method="post"><input name="__RequestVerificationToken" _
type="hidden" value="IP80d5XM-Qi0XQ1-IgGKGmhLVNGdtDAyM-r7lJ6yQCI1RIdJJph0uPnz-DzEHx12_booO4xwvcWg6EUWPiLnHv7ww6PD-aqfhiVxPcy-VYm6mnBRHsba3H7Hembliybo0" /> _
<div class="k-block k-info-colored">
<div class="k-header">
<span>Add Transfer Load Details</span>
</div>
<div class="k-content">
<div class="infocontainer">
<table>
<tr>
<td class="columnLabel">
<label for="From_District:">From District:</label>
</td>
<td class="columnData">
<input id="FromDistrict" name="FromDistrict" style="width: 200px" type="text" />
<script>
jQuery(function(){jQuery("#FromDistrict").kendoDropDownList({"dataSource" _
:{"transport":{"read": {"url":"/DistrictProfiles/GetUserDistricts","data": _
function() { return kendo.ui.DropDownList.requestData(jQuery("#FromDistrict")); }}, _
"prefix":""}, "serverFiltering":true,"filter":[],"error":OnError, _
"schema": {"errors":"Errors"}}, "dataTextField":"DistrictName","autoBind":true, _
"dataValueField":"DistrictCode", "optionLabel":"Select District..."});});
</script>
</td>
<td class="columnLabel"> 'Next dropdown section starts here
<label for="To_District:">To District:</label>
</td>
When the dropdown opens, it has 2 items to choose from, but nowhere in the code can I find those 2 items listed, so I'm assuming they're pulled from this line: return kendo.ui.DropDownList.requestData(jQuery("#FromDistrict")) , but I'm not sure. Can someone point out what I'm missing here ?
I did not post the "view element" because of the difficulty in copying it. All selections dynamically change other selection options.
IN the browser DOM explorer (which shows the markup 'computed/sanitized' by the rendering engine) you should see that
<input id="FromDistrict" name="FromDistrict" style="width: 200px" type="text" />
has been changed to include the datalist attribute. eg.
<input id="FromDistrict" name="FromDistrict" style="width: 200px" type="text" datalist="DistrictName" />
..... and further down the DOM, you should see the datalist element that has been injected into the DOM by the Kendo code.
<datalist id="DistrictName">
<option value="Kent">Kent</option>
<option value="Surry">Surry</option>
</datalist>
You should be able to automate the field, by just assigning a valid comma-separated list to the FromDistrict.
eg. FromDistrict.value='Kent, Surry';
I was able to accomplish what I wanted in a crude sort of way by using the following, but I'm working on a better more efficient way.
'Choose the FROM district
ie.Document.parentWindow.execScript "$('#FromDistrict').kendoDropDownList('open');"
Dim FrDist, li
Set FrDist = ie.Document.getElementById("FromDistrict-list").getElementsByTagName("li")
Dim fd
fd = 0
For Each li In FrDist
'MsgBox ("li.innertext is - " & li.innerText & " fd value is: " & fd)
If li.innerText Like "*KILGORE*" Then
FrDist(fd).Click
Else
'Do Nothing
End If
fd = fd + 1
'Application.Wait (Now + TimeValue("0:00:02"))
Next
Application.Wait (Now + TimeValue("0:00:02"))
'Choose the TO district
ie.Document.parentWindow.execScript "$('#ToDistrict').kendoDropDownList('open');"
Dim ToDist
Set ToDist = ie.Document.getElementById("ToDistrict-list").getElementsByTagName("li")
Dim tod
tod = 0
For Each li In ToDist
'MsgBox ("li.innertext is - " & li.innerText & " tod value is: " & tod)
If li.innerText Like "*KILGORE*" Then
ToDist(tod).Click
Else
'Do Nothing
End If
tod = tod + 1
Next

Filling in textbox on IE with excel macro, multiple name elements

I am try to enter a value from my excelsheet into a textbox in IE. Until now I am successful in opening the correct website.
Set IE = New InternetExplorerMedium
IE.navigate "http://test.com" 'dummysite
IE.Visible = True
IE.document.GetElementsByName("value") = "value from sheet"
To fill something in the textbox the element named "value" needs to be given a desired input. The problem is the this element name "value" occurs multiple times in the code of the website. I have tried something like (since it is the 7th element named value),
IE.document.GetElementsByName("value")(7) = "value from sheet"
but that does not work either. This is a part of the site source. I am trying to fill in the value element on the second row.
<td>
<input name="#_20_0_T" type="hidden" value="T_AN">
<input name="_20_0_T" type="text" size="40" value="">
</td>

Resources