Clicking an Angular (ng-click) search button in IE using excel VBA - vba

I am new to VBA. I am trying to click on the "GO" search button after I logged in to a secured website.
I tried many ways to click on the button, but I got no luck.
Here is the inspect element.
<button class="button primary pull-right" ng-click="search()" ng-disabled="searchForm.$invalid">
Go <i class="glyphicon glyphicon-chevron-right"></i>
</button>
Here is what I have tried.
Method 1
Set HTMLDoc = HTMLDoc.class("button primary pull-right").document
Set button = HTMLDoc.getElementsByTagName("BUTTON")(0) 'first button
button.Click
For i = 1 To 5
button.Click
DoEvents
Next
Method 2
For Each hyper_link In allhyperlinks
If hyper_link.class = "button primary pull-right" Then
hyper_link.Click
Exit For
End If
Next hyper_link
Method 3
Set allhyperlinks = IE.document.getElementsByTagName("button")
For Each hyper_link In allhyperlinks
If hyper_link.getAttribute("class") = "search" Then
hyper_link.Click
Exit For
End If
Next
Method 4
Dim oHTML_Element As IHTMLElement
Dim oBrowser As InternetExplorer
Dim ie As Variant
For Each oHTML_Element In ie.document.getElementsByName("button")
If oHTML_Element.className = "button primary pull-right" Then
oHTML_Element.Click
End If
Next
Method 5
Set ie = CreateObject("InternetExplorer.Application")
With ie
Do Until .readyState = 4
DoEvents
Loop
Application.Wait (Now + TimeValue("0:00:15"))
ie.document.getElementsByClassName("button primary pull-right").Click
Application.Wait (Now + TimeValue("0:00:1"))
End With
Method 6
Set ie = CreateObject("InternetExplorer.Application")
ie.document.getElementByClassName("button primary pull-right")(0).Click
Any help would be greatly appreciated.
Thank you!

I presume you have not given url because it is beyond a login. That makes solving ten times harder. Nevertheless some points.
ng-Click means its running Angular.
Sometimes it is required to make the control take the focus before simulating the Click event. Website does not recognize my inputs [how to fire IE dom event manually from VBA]
I'd use .querySelector() or .querySelectorAll() to acquire the button because I can use the path at the bottom of the Chrome Development Tools Window. VBA - Webscraping - jQuery selectors available with MSHTML's querySelector and querySelectorAll

Try this code
Sub Test()
Dim ie As Object
Dim e As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.navigate "www.jetblue.com"
Do Until .readyState = 4: DoEvents: Loop
For Each e In .document.getElementsByTagName("input")
If e.ID = "email_field" Then
e.Value = "your email"
ElseIf e.ID = "password_field" Then
e.Value = "your password"
End If
Next e
For Each e In .document.getElementsByTagName("input")
If e.ID = "signin_btn" And e.Type = "submit" Then e.Click: Exit For
Next e
End With
End Sub

Related

Catching anchor element using SHDocVw.InternetExplorer

I want to catch the anchor element in website via vba.
I tried both as an MSHTML.IHTMLAnchorElement and MSHMTL.IHTMLBUttonELement but either way gives an error on Set line. Can anyone describe what's wrong with code below?
Sub goToWhtSpp()
Dim ie As New SHDocVw.InternetExplorer
ie.Visible = True
ie.navigate "https://cooperatordirectory.com/"
Do While ie.readyState <> READYSTATE_COMPLETE Or ie.Busy
Loop
Dim doc As MSHTML.HTMLDocument
Set doc = ie.document
Application.Wait 3000
URL = "https://cooperatordirectory.com/search_results?location_value=Florida&adm_lvl_1_sn=FL&stateSearchLN=Florida&country_sn=US&location_type=administrative_area_level_1&stateSearch=FL&swlat=24.396308&nelat=31.000968&swlng=-87.634896&nelng=-79.974306&lat=27.6648274&lng=-81.5157535&faddress=Florida%2C+USA&place_id=ChIJvypWkWV2wYgR0E7HW9MTLvc"
ie.navigate URL
Dim aelement As MSHTML.IHTMLAnchorElement
Set aelement = doc.getElementsByClassName("btn btn-primary btn-block") ' err line
Dim btn As MSHTML.IHTMLButtonElement
Set btn = doc.getElementsByClassName("btn btn-primary btn-block") ' err line
Debug.Print btn.Value
End Sub
<a class="btn btn-primary btn-block" href="/pro/20220324090906/connect">Send Message</a>
You want a page load wait after each .navigate. In this case you only need the one .navigate and wait.
getElementsByClassName() returns a collection not a single node. So both your type declaration, subsequent SET attempt to this type, and later attempt to view the .value attribute will fail. .value is a property of a single node.
If you want the first "button" href then index into a correctly typed variable and call .item(0), or, more efficiently, use the querySelector() method of ie.document and pass in a single class of the multi-valued class (the one which still correctly identifies the target element).
Dim anchors As IHTMLElementCollection
Set anchors = .document.getElementsByClassName("btn-primary")
Dim anchor As IHTMLAnchorElement
Set anchor = .document.getElementsByClassName("btn-primary").Item(0)
Option Explicit
Public Sub PrintMessageUrl()
Const URL As String = "https://cooperatordirectory.com/search_results?location_value=Florida&adm_lvl_1_sn=FL&stateSearchLN=Florida&country_sn=US&location_type=administrative_area_level_1&stateSearch=FL&swlat=24.396308&nelat=31.000968&swlng=-87.634896&nelng=-79.974306&lat=27.6648274&lng=-81.5157535&faddress=Florida%2C+USA&place_id=ChIJvypWkWV2wYgR0E7HW9MTLvc"
Dim ie As SHDocVw.InternetExplorer
Set ie = New SHDocVw.InternetExplorer
With ie
.Visible = True
.navigate URL
Do While .readyState <> READYSTATE_COMPLETE Or .Busy: DoEvents: Loop
Debug.Print .document.querySelector(".btn-primary").href
.Quit
End With
End Sub

VBA Macro for Internet Explorer - code run well on debugging 1 by 1 command but didn't work if i run the whole program

I'm new with VBA Macro using for automate the internet explorer.
I've used it for creating bot to follow other online shopping follower (e-commerce).
Actually the code run well if I use debug 1 by 1 command, and it worked (can do the follow thing).
But if I run the whole program, the code just operate maybe several command & stopped (but it didn't error).
I used it for e-commerce (sho.ee).
The step is:
open IE for other shop's followers
grab all the link
open the new link in new IE
follow them, close the new IE
and looping.
If I run step by step it worked.
Hope you guys could help me which wrong with the code.
Here's the code => i changed the name of the shop by xxx
Sub Followers()
Dim objIE As InternetExplorer
Dim ieAnchors As Object
Dim Anchor As Object
Set objIE = New InternetExplorer
objIE.Visible = True
objIE.Navigate "https://shopee.co.id/shop/xxx/followers/"
Do While objIE.Busy = True Or objIE.ReadyState <> 4: DoEvents: Loop
Set ieAnchors = objIE.Document.getElementsByClassName("shop-href")
For Each Anchor In ieAnchors
result = Anchor
If result <> result_prev Then
Dim objIE_1 As InternetExplorer
Dim ieBtn As Object
Dim Btn As Object
Set objIE_1 = New InternetExplorer
objIE_1.Visible = True
objIE_1.Navigate Anchor
Do While objIE_1.Busy = True Or objIE_1.ReadyState <> 4: DoEvents: Loop
Set ieBtn = objIE_1.Document.getElementsByClassName("shopee-button-outline shopee-button-outline--complement shopee-button-outline--fill ")
For Each Btn In ieBtn
Btn.Click
Next Btn
objIE_1.Quit
result_prev = Anchor
End If
Next Anchor
End Sub
Sometimes pages load in parts, registering as complete before all elements have loaded. In these cases I use application.Wait to force the program to wait a set amount of time for page loads:
'wait five seconds
Application.Wait (Now + TimeValue("0:00:05"))
If it is a simple page, I will have a Do loop a check for ReadyState to avoid slowing the program down unnecessarily:
'wait for page to load
Do Until ie.ReadyState = READYSTATE_COMPLETE or ie.ReadyState = 4
DoEvents
Loop
yes it worked. by adding Application.Wait (Now + TimeValue("0:00:05"))
Sub Followers()
Dim objIE As InternetExplorer
Dim ieAnchors As Object
Dim Anchor As Object
Set objIE = New InternetExplorer
objIE.Visible = True
objIE.Navigate "https://shopee.co.id/shop/xxx/followers/"
Do While objIE.Busy = True Or objIE.ReadyState <> 4: DoEvents: Loop
Application.Wait (Now + TimeValue("0:00:02"))
Set ieAnchors = objIE.Document.getElementsByClassName("shop-href")
For Each Anchor In ieAnchors
result = Anchor
If result <> result_prev Then
Dim objIE_1 As InternetExplorer
Dim ieBtn As Object
Dim Btn As Object
Application.Wait (Now + TimeValue("0:00:02"))
Set objIE_1 = New InternetExplorer
objIE_1.Visible = True
objIE_1.Navigate Anchor
Do While objIE_1.Busy = True Or objIE_1.ReadyState <> 4: DoEvents: Loop
Set ieBtn = objIE_1.Document.getElementsByClassName("shopee-button-outline shopee-button-outline--complement shopee-button-outline--fill ")
For Each Btn In ieBtn
Btn.Click
Next Btn
objIE_1.Quit
result_prev = Anchor
End If
Next Anchor
End Sub

.Click action not doing anything - IE Simulation

I'm trying to simulate the interaction with Google through the IE app and going through the DOM to get the classes I need and all is fine, stepping though the code, except the .Click action which doesn't cause a crash but it doesn't do anything (page doesn't navigate) - Code and screenshot of HTML below:
Option Explicit
Private Sub Test_Automation()
Dim ie, doc, eInput, eButton, eButtons As Object
Dim sURL, sTest As String
Set ie = CreateObject("internetexplorer.application")
sURL = "https://www.google.co.uk/?gfe_rd=cr&ei=IpDvWK72LsjCaJCbjKAL&gws_rd=ssl"
sTest = "Test"
With ie
.Visible = True
.Navigate sURL
End With
Do While ie.Busy Or ie.readyState <> 4
DoEvents
Loop
Set doc = ie.document
Set eInput = doc.getElementByid("lst-ib")
Set eButtons = doc.getElementsByTagName("input")
eInput.Value = sTest
For Each eButton In eButtons
If (eButton.getattribute("name") = "btnK") Then
eButton.Click
Exit For
End If
Next
End Sub
Any advise on what I'm doing wrong would be great!
You can get rid of your For...Next loop at the bottom and replace it with this to click the button:
doc.forms(0).submit
The 0 can be changed to another number (such as 1 or 2) to click on a different button. If there are multiple buttons on a page that can be clicked on it will just take some trial and error to find out which number matches the button you want to click.

VBA click a specific button

I'm trying to get a program to access a website, type in a zip code, add the zip code then press a button to get to the next page. My code is below.
Now I'm stuck on this page and can't get the program to click on the "Property" tab and go to the next page I want to see. Can I get any help on clicking on this button?
I've tried .GetElementsbyID("").click but that doesn't seem to work...
Web page code around the button I want to press:
<span class="geographymap-span-tab" id="tab_PROPERTY_PAGE" onclick="showSearchTypeSection('PROPERTY_PAGE','PROPERTY')"><img id="PROPERTY_PAGE_IMG" src="/list/images/PROPERTY_PAGE_2.gif" alt="property" width="80" height="29" border="0" onmouseover="MM_swapImage('PROPERTY_PAGE')" onmouseout="MM_swapImgRestore('PROPERTY_PAGE')"></span>
My code thus far is below:
Sub TestProgram()
Dim i As Long
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
' You can uncoment Next line To see form results
IE.Visible = True
' Send the form data To URL As POST binary request
IE.Navigate "http://www.listsource.com/build.marketing.list"
' Statusbar
Application.StatusBar = "www.listsource.com is loading. Thanks and Gig 'em..."
' Application.StatusBar = False
' Wait while IE loading...
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
'Focus on the drop down menu
IE.document.getElementByID("locator").Focus
'Select zip code which happens to be the 19th item
IE.document.getElementByID("locator").selectedIndex = 19
'Get to the right page based on that selection
IE.document.getElementByID("locator").FireEvent ("onchange")
'input zipcode
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Application.StatusBar = "Search form submission. Please wait..."
Set objCollection = IE.document.getElementsByTagName("textarea")
i = 0
While i < objCollection.Length
If objCollection(i).Name = "zipTextArea" Then
' Set text for search
objCollection(i).Value = "75225"
Else
If objCollection(i).Type = "button" And _
objCollection(i).Name = "addZip" Then
' "Search" button is found
Set objElement = objCollection(i)
End If
End If
i = i + 1
Wend
' pull all elements that are buttons
Set objInputs = IE.document.getElementsByTagName("button")
'click button
For Each ele In objInputs
If ele.Name Like "addZip" Then
ele.Click
End If
Next
' Wait while IE re-loading...
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
' FIND OUT total # of SFR in each given zip code
' Click on "Property" button
End Sub
What can I add to click on the Property Tab?
Or you could check the form (searchform) for all elements named "addzip" and click on the first it encounters.
'(Dim frm as object)
'(Dim btnAdd as object)
Set frm = IE.document.forms("searchform")
Set btnAdd = frm.all("addzip")(0)
btnAdd.click

I need to find and press a button on a webpage using VBA

I have written a macro that will open a webpage, find the spots I need to put the data into, and then I want the macro to hit a prefill button, then hit Ok.
The page source code for the button is:
<input type="text" size="30" maxlength="40" name="name" id="name">
<input type="button" value="Prefill" onclick="prefill()">
I've been searching for answers all week and I think I have a basic understanding of how this is supposed to work by running a loop to search for it, but I'm having no luck in my endeavor of actually getting this to work.
Can someone show me the loop that will search my page for this button?
Thank you in advance.
Requested code so far
Private Sub Populate_Click()
Dim i As Long
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "website" 'make sure you've logged into the page
Do
DoEvents
Loop Until IE.READYSTATE = 3
Do
DoEvents
Loop Until IE.READYSTATE = 4
ActiveSheet.EnableCalculation = False
ActiveSheet.EnableCalculation = True
Call IE.document.getelementbyid("name").SetAttribute("value", ActiveSheet.Range("b2").Value)
Call IE.document.getelementbyid("aw_login").SetAttribute("value", ActiveSheet.Range("a2").Value)
Set objCollection = IE.document.getElementsByTagName("input")
i = 0
While i < objCollection.Length
If objCollection(i).Type = "button" And _
objCollection(i).Name = "Prefill" Then
Set objElement = objCollection(i)
End If
i = i + 1
Wend
objElement.Click
End Sub
Looks like you are pretty close, this is what I have used to do something similiar
With ie.document
Set elems = .getelementsbytagname("input")
For Each e In elems
If (e.getattribute("className") = "saveComment") Then
e.Click
Exit For
End If
Next e
End With
You will probably just have to change the if statement.
I also notice that your code refers to .Name = "Prefill" but your html snippet refers to .value = "Prefill"