VBA - IE automation - .getelements - excel

I'm trying to enter text into an input field that has no ID. When I inspect the element I get this:
<input title="Affected Customer(s)" class="custom-input ng-pristine ng-invalid ng-invalid-required ng-touched" role="combobox" aria-expanded="false" aria-required="true" aria-owns="typeahead-78-1606" aria-autocomplete="list" required="" type="text" placeholder="Enter customer name, email, login ID or corporate ID" entity="incident" field-name="customer.loginId" potentially-required-field="" auto-focus="" typeahead="user as user.firstName + ' ' + user.lastName for user in getList('person', $viewValue)" typeahead-loading="person.isLoading" typeahead-wait-ms="500" typeahead-on-select="onCustomerSelect($model)" typeahead-template-url="views/create/custom-add-user-dropdown.html" typeahead-min-length="3" prevent-click-event="" ng-model="person.ngModel">
how do I reference this input field? I have tried many ways but they all fail. I think my understanding is lacking and hopefully a bit of help will get me on the right track. The code I have now that tries to locate this input field is:
Sub Automate_IE_Load_Page()
'This will load a webpage in IE
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
'Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
'Set IE.Visible = True to make IE visible, or False for IE to run in the background
IE.Visible = True
'Define URL
URL = "https://ewpg-app-1041.hydro.mb.ca/ux/smart-it/#/create/incident"
'Navigate to URL
IE.navigate URL
' Wait while IE loading...
'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
Do While IE.readyState = 4: DoEvents: Loop 'Do While
Do Until IE.readyState = 4: DoEvents: Loop 'Do Until
objIE.Document.getElementsByName("customer.loginId")(0).Value = "test"
'click create new
'for each <a> element in the collection of objects with class of 'result__a'...
For Each aEle In objIE.Document.getElementsByName("Affected Customer(s)")
'...get the href link and print it to the sheet in col C, row y
result = aEle
'repeat times the # of ele's we have in the collection
Next
'Unload IE
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing
End Sub
The line "objIE.Document.getElementsByName("customer.loginId")(0).Value = "test"" gives me the error Run-time error 424 - object expected.
Thank you for any help you can provide, this will probably be extremely easy but I'm just not understanding something basic obviously. The code above was taken from a various sites, seems to work fine. I had the for look working for testing using different logic.

Try Below Code instead :
getElementsByClassName("custom-input ng-pristine ng-invalid ng-invalid-required ng-touched")

Related

Get values from HTML Input tag using vba

Following is the HTML code:
<input name="tb_country" type="text" value="Germany" readonly="readonly" id="tb_country" class="detailtextbox">
Following is the VBA Code:
Private Sub Btn_Click()
Dim IE As Object
Dim doc As HTMLDocument
DIM country As IHTMLElement
Set IE = New InternetExplorerMedium
IE.visible = True
IE.navigate "https://intranetsite.com"
Do While IE.readyState<>READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
Set doc=IE.document
Set Country = doc.getElementByID("tb_country")
Cells(3,2).value=Country.getAttribute("value")
IE.Quit
Set IE.Nothing
End Sub
But I am getting run time error 91
have tried many combinations like using class name and name, also have tried directly using country.value instead of getAttribute(), but still getting the same error.

Filling Internet Explorer Forms from Excel

I want to fill information onto the site 'select picker' section on the left side of the picture.
SITE
There is an element name 'picker' but it won't work.
The search bar is being completed using jQuery auto-complete which submits a form that navigates to an associate from the auto-complete box has been selected
I tried
IE.Document.All("picker").Value = "Testing"
IE.Document.GetElementByID("pickerCtrl.currentPicker").Value = "Testing"
IE.Document.GetElementsByClassName("input-group input-group-sm").Item(0).Value = "Testing"
PS.
Is there way to just copy and paste the row at the current position where cursor is positioning at?
I can sendkeys tab 18 times to get to that specific search bar but do not know how to insert the entire row from my Excel sheet.
eg. IE.Document.all("picker").Value = ThisWorkbook.Sheets("Main_1").Range("B" & intRow).Value
I am completely new to this.
Try to make a test with example below may help to solve the issue.
Sub demo1()
Dim i As Long
Dim URL As String
Dim IE As Object
Dim HWNDSrc As Long
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
URL = "D:\Backup20190913\tests\367.html"
IE.Navigate URL
Do While IE.ReadyState = 4: DoEvents: Loop
Do Until IE.ReadyState = 4: DoEvents: Loop
IE.Document.querySelector("[name='picker']").Value = "test value"
Stop
Set IE = Nothing
End Sub
HTML
<input class="form-control ng-valid ng-isolate-scope ng-touched ng-dirty" ng-model="pickerCtrl.currentpicker" name="picker" type="text" typeahead="" typeahead-source="pickerCtrl.getpickerStrings()" ng-disabled="pickerCtrl.errorLoadingPickers || pickerCtrl.noPickerstrings" ng-hide="pickerCtrl.disableTypeahead" autocomplete="off">
Output:

Excel VBA: Check all check boxes on Web Page

I'm trying to check all the checkboxes available on a webpage via VBA since the name convention doesnt appear to be one in which I can be selective. However I cannot seem to get anything to work. I can login to the website and navigate to the section of the website I want but cannot cross this hurdle. Any help would be greatly appreciate. Below is the source code from the webpage.
<li data-product-family="30yr"
data-product-amortizationTerm="30"
data-product-type="Conventional"
data-product-amortizationType="Fixed"
>
<label>
<input type="checkbox"
value="154232"
class="product-Conventional product-item"
data-authorized-remittance-types="ActualActual "
/>30-Year Fixed Rate - 110k Max Loan Amount</label>
</li>
VBA I attempted to write (edited)... code I'm using presently:
Public Sub TestIE()
Dim IE As Object
Dim aNodeList As Object, i As Long
' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
' You can uncoment Next line To see form results
IE.Visible = False
' Send the form data To URL As POST binary request
IE.Navigate "https://"
' Statusbar
Application.StatusBar = "Page is loading. Please wait..."
' Wait while IE loading...
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
IE.Visible = True
Set aNodeList = IE.document.querySelectorAll("input[type=checkbox]")
If aNodeList Is Nothing Then Exit Sub
For i = 0 To aNodeList.Length
aNodeList.Item(i).Checked = True
Next i
End Sub
You can try to get a nodeList of the checkboxes with:
IE.document.querySelectorAll("input[type=checkbox]")
You can traverse the nodeList along its .Length property.
E.g.
Dim aNodeList As Object, i As Long
Set aNodeList = IE.document.querySelectorAll("input[type=checkbox]")
If aNodeList Is Nothing Then Exit Sub
For i = 0 To aNodeList.Length -1
On Error Resume Next
aNodeList.item(i).Checked = True
On Error GoTo 0
Next i

Write in an HTML input field using Excel VBA

I load an internet page and log in with myusername and mypassword using VBA.
I would like to write a search-term in an input field of the webpage using VBA.
Because there is no name and ID in the input field, I didn't succeed.
Here is my code :
Sub MyLogin()
Dim IE As InternetExplorer
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.navigate "https://www.example.com"
Do Until .readyState = 4
DoEvents
Loop
.document.all.Item("username").Value = "myusername"
.document.all.Item("password").Value = "mypassword"
.document.forms(0).submit
End With
Do Until IE.readyState = 4
DoEvents
Loop
Dim inputfield As HTMLInputElement
For Each inputfield In IE.document.getElementsByTagName("input")
If inputfield.Type = "text" Then inputfield.Value = "mysearch"
Next
Do Until IE.readyState = 4
DoEvents
Loop
End Sub
The HTML field I want to fill:
input class="input-inboxsearch form-control" type="text" value="" placeholder="Nom, email" maxlength="71"
How can I set the value of the input field?
Further to #Miqi180's comment, you can try replacing your the section of your code starting with Dim inputfield As HTMLInputElement with this:
Dim inputfield As Object
Set inputfield = IE.document.getElementsByClassName("input-inboxsearch form-control")
If inputfield.length > 0 Then
inputfield(0).Value = "mysearch"
End If
'carry on with your code
The getElementsByClassName method will return a collection so you have to check its length and then access the items of the collection. If there is only one element with that class then you can reference the 0th element of the collection.
I've set the inputfield variable as an Object because you appear to be using late binding.

VB Click Button Macro Note Working

I am trying to get the results of tracking a package on my screen. In Excel, I have this code, which successfully opens the IE browser and successfully types the tracking number into the input box. What doesn't work is the last step - button click. I get the runtime error 91 - object variable or with block variable not set. Thanks in advance.
Sub TrackUSPS()
Const cURL = "https://tools.usps.com/go/TrackConfirmAction!input.action" 'Enter the web address here
Const trackNum = "9405511899560005266920" 'Tracking number
Dim IE As InternetExplorer
Dim doc As HTMLDocument
Dim LoginForm As HTMLFormElement
Dim TrackInputBox As HTMLInputElement
Dim TrackButton As HTMLInputButtonElement
Dim HTMLelement As IHTMLElement
Dim qt As QueryTable
Set IE = New InternetExplorer
IE.Visible = True
IE.navigate cURL
'Wait for initial page to load
Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
Set doc = IE.document
'Get the tracking form on the page
Set LoginForm = doc.forms(1)
'Get the tracking input box and populate it
'<input type="text" id="search-text" autocomplete="off" name="searchText" class="default" value="Search USPS.com or Track Packages" onclick="javascript:dojo.byId('search-text').value='';"/>
Set TrackInputBox = LoginForm.elements("search-text")
TrackInputBox.Value = trackNum
'Get the form input button and click it
'<input type="image" id="search-btn" src="/media/images/global/blank.gif" alt="Process Search" />
Set TrackButton = LoginForm.elements("search-btn")
TrackButton.Click
'Wait for the new page to load
Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
End Sub
This works for me, replace
Set TrackButton = LoginForm.elements("search-btn")
TrackButton.Click
with
IE.document.getelementbyID("search-btn").Click

Resources