Fill USPS webform dropdown using VBA EXCEL - excel

I am trying to automatically fill a USPS webform, and it is working for street address, city and zipcode, but I can't make it fill the State dropdown. Any ideas?
Here's the code I currently have:
Sub USPS()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https://tools.usps.com/go/ZipLookupAction!input.action?mode=1&refresh=true"
Do
DoEvents
Loop Until IE.READYSTATE = 4
Call IE.Document.getElementByID("tAddress").SetAttribute("value", "2 Peabody Terrace")
Call IE.Document.getElementByID("tCity").SetAttribute("value", "Cambridge")
Call IE.Document.getElementByID("sState").SetAttribute("value", "MA")
Call IE.Document.getElementByID("Zzip").SetAttribute("value", "02138")
Set AllHyperLinks = IE.Document.GetElementsByTagName("A")
For Each hyper_link In AllHyperLinks
If ID = "lookupZipFindBtn" Then
hyper_link.Click
Exit For
End If
Next
End Sub
Thank you very much for your help!

Make this change to your code:
'Call IE.Document.getElementByID("sState").SetAttribute("value", "MA")
With IE.Document.getElementByID("sState")
For i = 0 To .Length - 1
If .Item(i).Value = "MA" Then
.Item(i).Selected = True
Exit For
End If
Next
End With
This will work... but the web page will still show like the Select element has not changed. It's an illusion. I suspect there is some JavaScript in the page interfering with this element's display.
HOWEVER, if you do this in the Immediate Window of the VBEditor:
?IE.Document.getElementByID("sState").value
...you will see that the value for the control has indeed changed to 'MA'.
Further, if you go ahead and click on the web page's Find button, you will see that 'MA' was in fact included under 'You entered:'.
So my code above is the solution to your problem.

CSS selectors:
You can use css selectors to do this.
① The first selector I use is for the initial landing page to select the search by address:
#zip-lookup-app a > span
This says span tag within an a tag, inside element with id zip-lookup-app. "#" means id.
This matches the following, of which I want the first.
② The second selector:
#tState option[value='MA']
This says select option tag with attribute value, having value of 'MA', within element with id tState. The "[]" means attribute.
VBA:
As I want the first match I can use the querySelector method of document to apply the CSS selector as shown below:
Option Explicit
Public Sub MakeStateSelection()
Dim ie As New InternetExplorer, html As HTMLDocument
With ie
.Visible = True
.navigate "https://tools.usps.com/go/ZipLookupAction!input.action?mode=1&refresh=true"
While .Busy Or .READYSTATE < 4: DoEvents: Wend
Set html = .document
html.querySelector("#zip-lookup-app > div > div:nth-child(1) > div > ul > li:nth-child(1) > a > span").Click
While .Busy Or .READYSTATE < 4: DoEvents: Wend
html.querySelector("#tState option[value='MA']").Selected = True
'other code
Stop '<== Delete me
'.Quit '<== Uncomment me
End With
End Sub

Related

Click the button on the USPS Web Site to find 9 digit Zip Code

USPS ZipCode Website & Excel VBA -- I have all of the code working to populate the site, but I can not click the "FIND" submit button with Excel-VBA code.
Here is the URL for the site and all of the button clicking code I have submitted
https://tools.usps.com/zip-code-lookup.htm?byaddress
'All of the different submit Code I have tried:
html.all.getElementById("zip-by-address").Click
html.all.getelementbytagname("zip-by-address").Click
html.document.querySelector("Find[type=submit]").Click
html.document.getElementsByClassName("btn btn-primary").Click
html.document.getElementById("zip-by-address").Click
For Each Element In ie.document.getElementsByName("zip-by-address")
If Element.className = "btn-primary" Then
Element.Click
End If
Next
ie.document.querySelector("button[type=submit]").Click
For Each Element In ie.document.getElementsByTagName("zip-by-address")
If InStr(Element.innerText, "Find") > 0 Then Element.Click
Next
ie.document.getElementByTagName("Find").FireEvent "onclick"
ie.document.getElementById("zip-by-address").FireEvent "onkeypress"
ie.document.getElementById("zip-by-address").FireEvent "onclick"
Set frm = ie.document.forms(0)
frm.submit
I had to complete the city and state fields as well. You don't need to select the by address option as it is already part of the url.
.querySelector("#zip-by-address").Click is equivalent to .getElementById("zip-by-address").Click but faster as modern browsers (cough IE cough) are optimized for selecting by css selectors.
Public Sub ClickFindButton()
Dim ie As SHDocVw.InternetExplorer
Set ie = New SHDocVw.InternetExplorer
With ie
.Visible = True
.Navigate2 "https://tools.usps.com/zip-code-lookup.htm?byaddress" 'url is already by address so no need to click for it
Do
DoEvents
Loop While .Busy Or .ReadyState <> READYSTATE_COMPLETE
With .Document
.querySelector("#tCompany").Value = "The New York Times Company"
.querySelector("#tAddress").Value = "620 Eighth Avenue"
.querySelector("#tCity").Value = "New York"
.querySelector("[value=NY]").Selected = True
.querySelector("#zip-by-address").Click
Stop
End With
.Quit
End With
End Sub
Search by Id
Example
ie.Document.getElementById("zip-by-address").Click

Cannot perform auto search on a specific website

Recently I am learning to use excel macro to search on a website. I've read several forum threads and I came up with the code below. However, error appears when I reach the row
SearchBox(0).Value = SearchString
I tried to remove the (0) but another error appears as well. The code works well on other websites. How should I change them to adapt to this site?
P.S. I would also like to know the way to click the search button.
Sub Searchstockcode()
Dim SearchString As String
SearchString = "700"
Set ie = CreateObject("InternetExplorer.Application")
With ie
ie.Visible = True
End With
ie.Navigate "http://www.hkexnews.hk/listedco/listconews/advancedsearch/search_active_main.aspx"
While ie.ReadyState <> 4
DoEvents
Wend
Dim SearchBox As Object
Set SearchBox = ie.Document.GetElementsByName("ct100$txt_stock_code")
SearchBox(0).Value = SearchString
Dim SearchButton As Object
Set SearchButton = ie.Document.GetElementsByName
End Sub
I don't know whether the problem with your name selection is due to the element having two name attributes but that seems possible.
You may use the following.
For the search box I use its id to target the element. This is normally unique on a document and the fastest selector method.
For the search button I use a CSS attribute + value selector of
[src*='/image/search.gif']
This targets the src attribute [] of the element by its value. * means contains. The selector looks for a src attribute containing /image/search.gif in its value.
You can observe the attribute here:
Option Explicit
Sub Searchstockcode()
Dim SearchString As String, SearchBox As Object, SearchButton As Object, ie As Object
SearchString = "700"
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "http://www.hkexnews.hk/listedco/listconews/advancedsearch/search_active_main.aspx"
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
Set SearchBox = ie.document.getElementById("ctl00_txt_stock_code")
SearchBox.Value = SearchString
Set SearchButton = ie.document.querySelector("[src*='/image/search.gif']")
SearchButton.Click
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
Stop '<==Delete me
'other code
ie.Quit
End Sub

get background color from website

Hi im trying to get the backgound color from a website to compare it for other operations. Just like the Background color of the buy button of the web site.
Sub Colorfinder()
Dim oIE As New InternetExplorer
Dim oHtml As HTMLDocument
Dim tags As Object
Dim HTMLtags As IHTMLElementCollection
Dim HTMLtag As IHTMLElement
With oIE
.Visible = True
.navigate "https://www.gdax.com/trade/LTC-EUR"
Do Until .readyState = READYSTATE_COMPLETE: Loop
Set oHtml = .document
End With
'this is needed to wait until the page is totally loaded
Do: Set tags = oHtml.getElementsByClassName("OrderBookPanel_text_3fH-g")(0): DoEvents: Loop While tags Is Nothing
'getting the element that it is about
Do: Set HTMLtags = oHtml.getElementsByClassName("OrderForm_toggle_31S34"): DoEvents: Loop While HTMLtags.Length = 0
Set HTMLtag = HTMLtags(0).Children(0)
Debug.Print HTMLtag.innerText
'this is the problem
Debug.Print HTMLtag.Style.backgroundColor
End Sub
i got this idea from an otherstuck overflow user. but it didn´t work. link stuckoverflow
Try the below code:
Sub Colorfinder()
With CreateObject("InternetExplorer.Application")
.Visible = True
.navigate "https://www.gdax.com/trade/LTC-EUR"
Do While .Busy Or .readyState < 4: DoEvents: Loop
With .Document
Do While .getElementsByClassName("OrderBookPanel_text_3fH-g").Length = 0: DoEvents: Loop
Do While .getElementsByClassName("OrderForm_toggle_31S34").Length = 0: DoEvents: Loop
With .parentWindow
.execScript "var e = document.getElementsByClassName('OrderForm_toggle_31S34')[0].children[0];"
.execScript "e.style.backgroundColor = window.getComputedStyle(e,null).getPropertyValue('background-color');"
End With
Debug.Print .getElementsByClassName("OrderForm_toggle_31S34")(0).Children(0).Style.backgroundColor ' rgb(77, 165, 60)
End With
End With
End Sub
.execScript executes JScript code within HTML document, that is why syntax uses var, ', [] and ;, etc., variable e is the target node declared in the document scope, and it's actual computed background value just put into .backgroundColor property. Using .execScript is the only way I found to get to window.getComputedStyle method, which actually do the job.
Is this really something that needs to be automated?
The background-color of the BUY button is:
(Click for more about #4da53c Strong Sap Green.)
Right-click any element on a web page and choose Inpsect Element to find the color under the style properties (specific location depends on your browser).
Or, use a color detection utility such as:
Instant Eyedropper which runs in the background. You click the icon in your system tray and then any spot on your screen to identify the color of the pixel.
Firefox add-on: colorPicker same idea, but a plug-in.
Others are available for other browsers, and there are also websites that will identify colors from uploaded images, URLs, entire website color schemes, etc.

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

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