VBA how to fill in search bar with ID? - excel

Web page has the following HTML without an ID - how do I set the value of the form?
<input name="loanxFindBorrower" onkeypress="if((window.event&&window.event.keyCode==13) ||
(event.which&&event.which==13 )){ findByBorrowerAction()}" type="text" size="25" value="">
I've tried
IE.Document.getElementByTagName("loanxFindBorrower").Value = "New Value"
Aswell as
IE.Document.getElementByTagName("loanxFindBorrower").Focus
IE.Document.getElementByTagName("loanxFindBorrower").Value = "NewValue"
Any help would be much appreciated

You should use getElementsByName() to get the element. It returns a collection, you'll need to specify the index to get the element you want.
For example, if it's the first element with name "loanxFindBorrower" in the page, the index is 0:
IE.Document.getElementsByName("loanxFindBorrower")(0).Value = "New Value"

Related

VBA - Trying to Input text and click Search button, All within a Table, within a Form, No ID Element, only Class Element

Trying to input a Value into an Input Box and press the Search button to get the results from a table.
I can't seem to figure out how to input the Value into the Box. The input box and button seems to be inside a form inside a table. No ID element.
**> Input Box has these properties**
<td Class = "searchRow">
<input type = "text" name = "name" value onblur = "Numberfield();" style = "width: 200px;" class = "required">
This is all inside Form ID "SearchForm" > div ID "Search" > Tr > Td CLass "searchRow"
After I grab the correct internet explorer tab, I need to getElementByClassName and inp9ut value
IE.Document.getElementsByTagName("tr").getElementsByTagName("input").Value = "Search"
IE.Document.getElementsByClassName("searchRow").getElementsByTagName("input").Value = "Search"
I can't seem to get any combination to work.
Another element is the Button.
Button has these properties
<td colspan = "6" style = "align:right;" class = "searchonly">
<input type = "button" class = "actionbutton" value = "search" onclick = "submitform()">
How do i Click this button that is in the Table Row.
IE.Document.getElementsByClassName("searchonly").getElementsByTagName("actionbutton").Click
Does this make sense?
Appreciate the help.
Better to add ID attribute and then use getElementsByID.
And If that is not possible then you can use XPATH and get the element by using following function.
function getElementByXpath(path) {
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
console.log( getElementByXpath("VALUE OF XPATH") );
And you can easily get the XPATH by using the extension in any browser. Like XPath Finder available in google chrome.
I write the Answer as comments and code:
'What you want first
'**> Input Box has these properties**
'<td Class = "searchRow">
' <input type = "text" name = "name" value onblur = "Numberfield();" style = "width: 200px;" class = "required">
'This is all inside Form ID "SearchForm" > div ID "Search" > Tr > Td CLass "searchRow"
'What you try
'IE.Document.getElementsByTagName("tr").getElementsByTagName("input").Value = "Search"
'IE.Document.getElementsByClassName("searchRow").getElementsByTagName("input").Value = "Search"
'What should work
'Attention: the get methods work case sensitive. It's a difference between "Search" and "search"
'I write that because I wonder if the first letter of the ID "Search" is upper case in the original document
'
'You can seperate the div tag include all inner tags as own DOM object
Dim nodeDivSearch As Object
Set nodeDivSearch = IE.Document.getElementByID("Search")
'
'Now you can work on the new DOM object only
'Note that the getElements methods always create a NodeCollection
'All elements of the collection have an index. The first element always has
'index 0, so you can access the first input tag in the new DOM object as follows
'(getElementByID does not create a NodeCollection, because an ID should always
'be unique. So there is only one element for this criterion anyway)
nodeDivSearch.getElementsByTagName("input")(0).Value = "Search"
'
'I think your code will work with setting indexes
IE.Document.getElementsByClassName("searchRow")(0).getElementsByTagName("input")(0).Value = "Search"
'What you want second
'Button has these properties
'<td colspan = "6" style = "align:right;" class = "searchonly">
' <input type = "button" class = "actionbutton" value = "search" onclick = "submitform()">
'What you try:
'IE.Document.getElementsByClassName("searchonly").getElementsByTagName("actionbutton").Click
'I think you know what to do here
IE.Document.getElementsByClassName("searchonly")(0).getElementsByTagName("actionbutton")(0).Click
getElementsByClassName returns an array-like object of all child elements which have all of the given class names. So you need to choose which element you need using index in the returned array. So does it with getElementsByTagName.
So if the html code is like below:
<form id="SearchForm">
<div id="Search">
<table>
<tr>
<td Class="searchRow">
<input type="text" name="name" value onblur="Numberfield();" style="width: 200px;" class="required" id="a1">
</td>
<td colspan="6" style="align:right;" class="searchonly">
<input type="button" class="actionbutton" value="search" onclick="submitform()">
</td>
</tr>
</table>
</div>
</form>
Then the vba code to set value and click button should be like below:
IE.Document.getElementsByClassName("searchRow")(0).getElementsByTagName("input")(0).Value = "Search"
IE.Document.getElementsByClassName("searchonly")(0).getElementsByClassName("actionbutton")(0).Click
The code above is just for example, you should change the index according to the actual situation of the website you visit.

working with .Document.getElementById() and variables in vba

I am trying to select the value from a dropdown box using vba, the code block for the dropdown box is as follows
<input type="text" id="form_autocomplete_input-1542902425322" list="form_autocomplete_suggestions-1542902425322" placeholder="Search keyword or select filter" role="combobox" aria-expanded="false" autocomplete="off" autocorrect="off" autocapitalize="off" aria-autocomplete="list" aria-owns="form_autocomplete_suggestions-1542902425322 form_autocomplete_selection-1542902425322">
If the value of form_autocomplete_suggestions-1542902425322 was static I would use .Document.getElementById("form_autocomplete_suggestions-1542902425322").Value = "Role: Student" however this seems to be a randomly generated numerical value.
I have had a look and it seems I cannot simply add a wildcard in such as .Document.getElementById("form_autocomplete_suggestions-*").Value = "Role: Student"
And as its randomly generated and such a long number it cannot loop through an array of values. so I am unsure on how to solve this issue.
You can use css attribute equals value selector syntax with the ^ operator to say starts with a certain substring. You could also use * instead, which means contains.
[id^='form_autocomplete_input-']
VBA:
ie.document.querySelector("[id^='form_autocomplete_input-']")
You might also use:
[placeholder='Search keyword or select filter']
Which would be:
ie.document.querySelector("[placeholder='Search keyword or select filter']")
As you indicate this needs to be selected you may need:
ie.document.querySelector("[id^='form_autocomplete_input-']").Selected = True
Reference:
CSS attribute selectors

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>

setting textarea text based on current input field

I have two input fields
input(ng-model='form.firstName', name='firstName', id='familyName')
and
input(ng-model='form.lastName', name='lastName')
I also have a textarea field
textarea(id='fieldInfo', ng-model='fieldInfo', name='fieldInfo' cols='15', rows='10')
I would like to change the text in the TextArea to "Please enter your first name" when the focus or cursor is in the First Name input and change it to "Please enter your last name" when the is on the last name inout and it should be able to toggle as the user switches focus from first name to last name and last name to first name.
Any assistance will be much appreciated.
Melroy
The ng-focus directive will not work for you if you are using one of the stable 1.0.x versions of AngularJS. However, I have created a working CodePen example of how you could do it without the ng-focus directive.
I created my own on-focus directive that evaluates an expression, which results in an update to the scope model being used by the textarea. I'm sure it could use some refinement, but the concept should still work for your application.
You can use the ngFocus directive if you are using angular 1.2.0.
In your controller:
var myApp = angular.module('myApp',[]);
myApp.controller('MyCtrl', ['$scope', function($scope) {
$scope.text = "";
}]);
And in your view:
<input type="text" data-ng-model="form.name" ng-focus="text = 'Please enter your first name'" />
<input type="text" data-ng-model="form.last" ng-focus="text = 'Please enter your last name'" />
<textarea name="area" id="textarea" cols="30" rows="10"> {{ text }} </textarea>
You can check this fiddle
Edit: Wrong fiddle link

How to "document.write();" on the same page?

Basically, I have made a form which allows you to input 2 numbers, and when you press the 'Add' button, the program writes the answer onto the screen, the only thing is when the answer is written, it appears on a separate page. How do I get it to write to the same page, below is the HTML code:
<form type="twoNum" method="get">
<input type="float" placeholder="Enter first number here..." name="num1" id="n1"/>
<input type="float" placeholder="Enter second number here..." name="num2" id="n2"/>
<input type="button" value="Add" name="sndfunct" onClick="twoNum(this.form);"/>
</form>
Below is the Javascript code:
function twoNum(form)
{
var num1 = form.num1.value;
var num2 = form.num2.value;
var intNum1 = parseFloat(num1);
var intNum2 = parseFloat(num2);
document.writeln(intNum1 + intNum2);
}
Please note that using document.write() is considered bad practice. E.g. see the warning on the W3C web site.
Furthermore, you can’t use it to edit a closed document. document.write() can only be used while the document is being loaded.
In order to do what you want, you should have a <span id="foo"></span> somewhere in your document, and then do:
document.getElementById("foo").textContent = intNum1 + intNum2;
This will insert your number inside your span element. Actually, it replaces the content of the (previously empty) span element.
Edit: Of course, it can be any kind of element. I used a span element just for the example.
You can write <p id="answer"></p> in the form. Then create var answer=intNum1 + intNum2 and after that instead document.writeln(intNum1 + intNum2) write document.getElementById("answer").innerHTML=answer!

Resources