Filling Internet Explorer Forms from Excel - 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:

Related

IE Handling in Excel macros

I am new to Excel macros and I am trying to auto-fill the text box and select the option from Dropdown and click on Submit.
Using macros I have navigated to a page and from the page, I need to enter the text in the text box, and based on the Suggestion, I need to select the option.
Once the option is selected I need to click on continue using macros.
Here is the current code (I tried with the commented line but it's not working).
Sub Create_Change()
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Dim HWNDSrc As Long
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
URL = "URL to Navigate"
IE.Navigate URL
Application.StatusBar = URL & " is loading. Please wait..."
Do While IE.ReadyState = 4: DoEvents: Loop
Do Until IE.ReadyState = 4: DoEvents: Loop
Application.StatusBar = URL & " Loaded"
' IE.Document.All("Search for a template").Value = "text to search"
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing
End Sub
HTML CODE:
<input title="Search for a template"
class="change-template__search-input ng-pristine ng-untouched ng-valid"
role="combobox"
aria-expanded="false"
aria-owns="typeahead-258-8737"
aria-autocomplete="list"
type="text"
placeholder="Search for a template"
ng-enter="getRecommendedTemplates(template.search)"
ng-model="template.search"
typeahead="template as template for template in getTemplateList($viewValue)"
typeahead-focus-first="false"
typeahead-on-select="getRecommendedTemplates($item)"
typeahead-min-length="3">
I think you need to search through all the elements returned by Document.All(). Try looping through the collection that is returned. This is much easier if the control had an id, but you will have to check each element having a title = "Search for a template". Once you have found the item you can set the text.
I suggest debugging so you can see what .All returns and then you can work from there.
welcome to SO. If you need to get an element, as drHodge said, using an iterator and some sort of ID attribute is usually enough. I tried this with Google a minute ago:
Dim Item As Object
For Each Item In IE.Document.all()
If Item.getAttribute("title") = "Buscar" Then
Item.Value = "Test"
End If
Next Item
I also made it press space, down arrow and enter, for the sake of doing something with the suggestion and searching part:
SendKeys ("{ }")
Application.Wait Now + TimeSerial(0, 0, 1)
SendKeys ("{DOWN}"), True
Application.Wait Now + TimeSerial(0, 0, 1)
SendKeys ("{ENTER}"), True
(I do not recommend using SendKeys on any script, tho; it's quite unreliable.)
You could try the following:
1) Focus and change
With ie.document.querySelector("[title='Search for a template']")
.focus
.value = "abcd"
End With
2) Javascript set value
ie.document.parentWindow.execScript "document.querySelector('[title=\'Search for a template\']').value = 'abcd';"
3) Attach event and fire
Dim evt As Object
Set evt = ie.document.createEvent("HTMLEvents")
evt.initEvent "change", True, False
With ie.document.querySelector("[title='Search for a template']")
.Focus
.Value = "abcd"
.dispatchEvent evt
End With
4) FireEvent
With ie.document.querySelector("[title='Search for a template']")
.Focus
.Value = "abcd"
.FireEvent "onchange"
End With

Filling forms in an IE Window: Press buttons and fill in text boxes

I am trying to automate filling in an IE site that is repetitive and time consuming.
I wrote code that launches IE, makes it visible to the user, Dims the variables that it needs to fill in.
I have found the ID and the name of the text box but I cannot fill it in.
The site is a corporate site so I cannot attach the link.
I can attach the HTLM code of the box I am trying to fill and button I'm am trying to click.
TEXT BOX
<input name="ctl00$m$g_8fbfd2b6_3945_4bee_9a3b_20cfc2c846af$ctl00$grdLineItems$ctl02$txtDescription"
type="text"
id="ctl00_m_g_8fbfd2b6_3945_4bee_9a3b_20cfc2c846af_ctl00_grdLineItems_ctl02_txtDescription"
class="txtDescription"
onkeydown="return (event.keyCode!=13);"
style="width:680px;margin-top:2px;margin-bottom:2px" />
BUTTON CLICK
<input type="submit"
name="ctl00$m$g_8fbfd2b6_3945_4bee_9a3b_20cfc2c846af$ctl00$ButtonAdd"
value="Add New Item"
id="ctl00_m_g_8fbfd2b6_3945_4bee_9a3b_20cfc2c846af_ctl00_ButtonAdd"
style="color:Black;background-color:White;font-size:Medium;font-weight:bold;width:150px;"/>
I tried different ways. I tried getting the text box by name but I am getting an error on the document command.
MY CODE
Sub Fill_in_eShipper()
Dim IE As Object Dim web_address As String
Dim number_of_elements As Integer
Dim MydataSet As Variant
Dim ObjCollection As Object
Windows("eshipper filler.xlsm").Activate
Set IE = CreateObject("InternetExplorer.Application")
web_address = Range("c2") Range("D2").Formula = "=ROWS(A1:A161)-1"
number_of_elements = Range("d2")
IE.navigate web_address
IE.Visible = True
While IE.Busy DoEvents 'Wait till IE is done loading Wend
'Number of elements to populate
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range("D1").Select
ActiveCell.FormulaR1C1 = "=ROWS(RC[-3]:R[160]C[-3])-1"
IE.document.getelementsbyid("ctl00_m_g_8fbfd2b6_3945_4bee_9a3b_20cfc2c846af_ctl00_grdLineItems_ctl02_txtDescription").Value = Range("A2")
IE.document.All("ctl00$m$g_8fbfd2b6_3945_4bee_9a3b_20cfc2c846af$ctl00$ButtonAdd").Click
End Sub
Try - changed Elements to Element and just stuck with .getElementById for the click as well.
IE.document.getElementById("ctl00_m_g_8fbfd2b6_3945_4bee_9a3b_20cfc2c846af_ctl00_grdLineItems_ctl02_txtDescription").Value = Range("A2")
IE.document.getElementById("ctl00_m_g_8fbfd2b6_3945_4bee_9a3b_20cfc2c846af_ctl00_ButtonAdd").Click
You have been given the best selector method in the existing answer, which is to use the element id attributes.
Here is alternate syntax for the same thing using the CSS on the page and id selectors ("#").
With ie.document
.querySelector("#ctl00_m_g_8fbfd2b6_3945_4bee_9a3b_20cfc2c846af_ctl00_grdLineItems_ctl02_txtDescription").Value = Range("A2")
.querySelector("#ctl00_m_g_8fbfd2b6_3945_4bee_9a3b_20cfc2c846af_ctl00_ButtonAdd").Click
End With

VBA Clicking Button on IE

I use VBA a lot, but I'm still a beginner when it comes to IE automation.
I need to create a code to click a "button" on a specific website for my company.
This button opens up a small menu where I need to select a few things (I'll deal with that later I guess). Thing is, the button doesn't show any ID or name, it's a span and I'm having trouble making it click it.
Below is the HTML when I press F12 and inspect the element over the button:
<span class="search_for_watchers">
Procurar por outros observadores para adiconar
</span>
I've tried so many suggestions that I found over the internet.. I've tried getting the element by ID, name, class name, etc, but no success.
Below is the code I'm using:
Sub Automate_IE_Load_Page()
Dim i As Long
Dim URL As String
Dim ie As Object
Dim objElement As Object
Dim objCollection As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
URL = "Examplesite.com"
ie.Navigate URL
Application.StatusBar = URL & " is loading. Please wait..."
Do While ie.READYSTATE = 4: DoEvents: Loop
Do Until ie.READYSTATE = 4: DoEvents: Loop
Application.StatusBar = URL & " Loaded"
botao = ie.Document.getElementsByClassName("search_for_watchers")
botao.Click
End Sub
It doesn't show any error.. it just doesn't open up the sub-menu I want to, therefore the button isn't clicked.
Thanks!
getElementsByClassName is plural; i.e. it represents a collection. The following represents the first object with this class then clicks the first anchor tag within.
ie.Document.getElementsByClassName("search_for_watchers")(0).getElementsByTagName("a")(0).click
If you want to assign an object var to the element and then use the var to click, you need to Set the var.
You enter endless loop here twice:
Do While ie.READYSTATE = 4: DoEvents: Loop
Do Until ie.READYSTATE = 4: DoEvents: Loop
E.g., it looks like this:
Do While ie.READYSTATE = 4
DoEvents
Loop
but the : are instead of a new line in VBA.
Make sure to change a bit the loop, thus something like this:
Do While NOT ie.READYSTATE = 4: DoEvents: Loop
You could also loop through the tags and find the one you want to click, like so:
Set tags = ie.document.getElementsByTagName("a")
For Each tagx In tags
If tagx.innerText = "Procurar por outros observadores para adiconar" Then
tagx.Click
Exit For
End If
Next

VBA: It only shows ID at Ispect Element

I'm a newbie at VBA programming, and I use it to build macro a in excel. Here is this webpage: http://www.eppraisal.com/home-values/property/14032-s-atlantic-ave-riverdale-il-60827-59143864/
I want to extract the value of $52,920 on the right upper corner. When I select the Inspect Element, this is the following:
<p id="eppraisalval">$52,920</p>
I checked the Page Source, but I couldn't find it. The closest thing I found:
<span id="eppraisal_val" class="valuation-estimates-price ajaxload" data-url="/home-values/property_lookup_eppraisal?a=14032%20S%20Atlantic%20Ave&z=60827&propid=59143864">loading...</span>
I tried the get getElementById("eppraisal_val") and getElementById("eppraisalval"), but none of those worked.
How can I address my code to get that element?
Here is more from the inspect element window, aroun that code:
<span id="eppraisal_val" class="valuation-estimates-price ajaxload" data-url="/home-values/property_lookup_eppraisal?a=14032%20S%20Atlantic%20Ave&z=60827&propid=59143864"><p id="eppraisalval">$52,920</p><p class="main-page-description-small valuation_details" style="display:none;margin-top:5px">Low: $44,982 <br>High: $60,858</p></span>
Here is the shorter version of code I tried:
Sub macroID()
On Error Resume Next
Dim ie As Object
Set ie = CreateObject("internetexplorer.application")
Dim doc As HTMLDocument
Dim valuation As String
Dim val1 As String, val2 As String
Set doc = ie.Document
ie.Visible = True
ie.navigate "http://www.eppraisal.com/home-values/property/14032-s-atlantic-ave-riverdale-il-60827-59143864/"
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE
Set doc = ie.Document
valuation = doc.getElementById("eppraisalval")
Cells(1, 4).Value = valuation
Application.Wait (Now + TimeValue("0:00:03"))
End Sub
The issue with scraping from that URL is that there is a verification page before hand which requires you to confirm you "are not a robot" which makes it hard to scrape anything from it.
If you manually do this first it may save this in your cache and then allow you to run macros freely to scrape the website however you'd have to try this out.
In the meantime, the only issue I could see with your code is that you haven't included .innertext after .getElementById("eppraisalval"). The valuation line should look like this:
valuation = doc.getElementById("eppraisalval").innerText

Scraping on Amazon Prime Now postcode entry

I'm looking to enter a postcode on primenow.amazon.co.uk submit the form and paste the results into excel
Sub PostCode_Delivery_Short()
Dim ie As Object
Dim form As Variant, button As Variant
'add the “Microsoft Internet Controls” reference in your VBA Project indirectly
Set ie = CreateObject("InternetExplorer.Application")
'Input box for postcode
Postcode = InputBox("Enter Postcode")
With ie
.Visible = True
.Navigate ("primenow.amazon.co.uk")
'we ensure that the web page downloads completely before we fill the form automatically
Application.Wait (Now + TimeValue("00:00:04"))
'assigning the vinput variables to the html elements of the form
ie.Document.getElementsByClassName("availability__form__instructions__heading")(0).innertext = Postcode
'accessing the button via the form
Set form = ie.Document.getElementsByClassName("form")
Set button = form(0).onsubmit
form(0).submit
End With
'cleaning up memory
Set ie = Nothing
End Sub
What I'm struggling with is element ID (I think), I keep getting a runtime "error of Object variable or With block variable not set".
getElementsByName returns a collection, not a single element, so try something like:
ie.Document.getElementsByName("prime-now-input")(0).innertext = Postcode
EDIT Also:
.Document.getElementsByTagName("availability__form__instructions__heading")
I'm pretty sure there's no HTML element with that tag name ;-)
Maybe you meant getElementsByClassName() ?
EDIT2: this is the element where you need to input the postcode (using .Value, not .innerText)
<input type="text"
placeholder="Enter a postcode to see if we deliver to your area ..."
maxlength="9" data-reactid=".0.1.0.1.1.0.0.1.0.0">
My version of IE doesn't even render the input, so I can't offer more suggestions.
The website may have update and the below is for U.K. but it enters a postcode and presses the Shop Now (submit) button.
I use a CSS selector of input[type='submit'] to target the submit button. It reads as element(s) with input tag having type attribute with value 'submit'. The "[]" means attribute. Using the querySelector method to apply this selector will retrieve only the first match, as required.
Option Explicit
Public Sub SubmitPostCode()
Dim ie As New InternetExplorer
Const URL As String = "https://primenow.amazon.co.uk/onboard?sourceUrl=%2F"
Const POSTCODE As String = "WC1A 1DG"
With ie
.Visible = True
.navigate URL
While .Busy Or .readyState < 4: DoEvents: Wend
.document.getElementById("lsPostalCode").Value = POSTCODE
.document.querySelector("input[type='submit']").Click
While .Busy Or .readyState < 4: DoEvents: Wend
Stop '<== Delete me
.Quit
End With
End Sub

Resources