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

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.

Related

VBA how to fill in search bar with ID?

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"

Selecting a webpage radio-button

I am trying to do some webpage data extraction using VBA within excel. I have managed to automate the login process, which takes me to a new page with a search form. This form has an input field for 'Search Value' and a series of radio buttons that specify the variable to conduct the search on eg. ID number, Name. I am trying to automate filling in the input field and selecting one of the radio buttons and then submitting the form.
The code I used to login does not seem to work on the next page where I need to do the search. This is the HTML from the search page containing the input field:
<table width="100%">
<tr>
<td style="font-weight: bold; width:10%;" nowrap="nowrap">Search Value</td>
<td style="width: 15%; text-align: left;">
<input name="txtSearch" type="text" id="txtSearch" onkeyup="return txtSearch_onkeyup()" style="background-color:White;border-color:#222244;border-width:1px;border-style:Solid;height:20px;width:125px;" />
Which I try to fill in using:
ieDoc.Document.getElementsByName("txtSearch")(0).Value = "Test String"
Or:
ieDoc.Document.all.txtSearch.Value = "Test String"
Or
ieDoc.Document.getElementByID("txtSearch").Value = "Test String"
...all giving me the same Object defined error.
I have confirmed that the ieDoc is referencing the correct page after the login (by checking the title in the immediate window), and tried to ensure it is not a timing issue with page-loading.
I know the HTML methods in vba tend to be temperamental but I've run out of ideas. Any other way to access the input field on this page?
Success!
I was trying to reference an element on a sub-frame within the IE object, which I accessed with:
ieDoc.Document.frames(0).Document.all.txtSearch.Value = "Hello!"

Click on outer element with inner element with specific text in Puppeteer

How can I click on an outer element that contains a span with specific text via puppeteer?
For example I have the following HTML code:
<div role="button">
<div>
<span>
<span>OK</span>
</span>
</div>
</div>
And I would like to click on the most outer element (role = button). In order to click on the span with the Ok text I would do the next thing:
const [button] = await page.$x("//span[contains(., 'Ok')]");
if (button) {
await button.click();
}
But how can I click on the outer element using this text identifier?
You can use the descendant expression to query for child elements. Quote from the link:
The descendant axis indicates all of the children of the context node, and all of their children, and so forth.
Your XPath expression then looks like this:
//div[#role='button' and descendant::span[contains(., 'Ok')]]
Depending on your use case you might also want to check out this information about the difference of contains(text(), ...) and contains(., ...).

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>

Resources