How to click on Submit button using VBA - excel

I have a webpage on which i want to autologin and click on submit button using vba the code is below
'start a new subroutine called SearchBot
Sub SearchBot()
'dimension (declare or set aside memory for) our variables
Dim objIE As InternetExplorer 'special object variable representing the IE browser
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
Dim y As Integer 'integer variable we'll use as a counter
Dim result As String 'string variable that will hold our result link
'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer
'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True
'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "mywebpage"
'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
'in the search box put cell "A2" value, the word "in" and cell "C1" value
objIE.document.getElementById("loginID").Value = ("userid")
objIE.document.getElementById("password").Value = ("password")
after this code i want to click on submit button using vba
HTML inspect element code is below
<INPUT class=btn2 type=submit value=Submit name=submit1>

Try finding element by name and click it.
objIE.document.getElementsByName("submit1")(0).click

Related

How do I place focus and click submit button using VBA in Excel?

I am new to web scraping and VBA but am eager to learn further.
I have a small project in mind and am taking it slowly one step at a time.
I have a small piece of code in an Excel module which is almost working. I just cannot get it to submit my search box text.
I basically have the example horse name in Cell A2 in my Excel worksheet. The code opens the website and also enters the text into the search box.
I just can't get it to click the Go button.
I'd appreciate any help and an explanation of what I'm doing wrong so I can avoid it again!
When inspecting the Go button on the site, the HTML is thus:
My code:
Sub HorseSearch()
'define objIE
Dim objIE As InternetExplorer
'create an instance objIE
Set objIE = New InternetExplorer
'make web page visible
objIE.Visible = True
'navigate objIE to this web page
objIE.navigate "https://www.britishhorseracing.com/racing/horses/racehorse-search-results/"
'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
'in the search box put cell "A2" value which holds a horse name Tiger Roll
objIE.document.getElementById("text-search").Value = Sheets("Sheet1").Range("A2").Value
'place focus on the Go button
objIE.document.getElementById("Submit").Focus
'click the 'go' button
objIE.document.getElementById("Submit").Click
End Sub
Improving your code
Your problem is that when you fire the click on the submit button it is not enabled. You have to enable it. If you observe it the submit button got enabled when you manually insert a text into the input box, with a length > 3 characters.
Your code insert the text changing the value attribute of the input box but it doesn't fire the 'onchange' event.
Using web developer tools in Chrome I saw that the form behaviour (input box vs submit button) is built in jQuery.
One of the possible solutions can be to use
objIE.Document.parentWindow.ExecScript "jQuery(""#text-search"").trigger(""change"")"
after inserting the value and before clicking on the submit button.
Here the complete code:
Sub HorseSearch()
'Dim objIE As Object
'Set objIE = CreateObject("InternetExplorer.Application")
Dim obJe As InternetExplorer
Set objIE = New InternetExplorer
'make web page visible
objIE.Visible = True
'navigate objIE to this web page
objIE.navigate "https://www.britishhorseracing.com/racing/horses/racehorse-search-results/"
'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
'in the search box put cell "A2" value which holds a horse name Tiger Roll
objIE.Document.getElementById("text-search").Value = Sheets("Sheet1").Range("A2").Value
On Error Resume Next
objIE.Document.parentWindow.ExecScript "jQuery(""#text-search"").trigger(""change"")"
On Error GoTo 0
'click the 'go' button
objIE.Document.getElementById("Submit").Click
End Sub
Another solution
Why to use the form when we can directly go to the result page?
Result page is something like
https://www.britishhorseracing.com/racing/horses/racehorse-search-results/#!?pagenum=1&q=tiger%20roll&rated=false
where the q parameter value is the text you put in the input box. We could navigate directly to that page (using the value from cell A22 for q parameter in the URL).
Sub HorseSearch()
'Dim objIE As Object
'Set objIE = CreateObject("InternetExplorer.Application")
Dim obJe As InternetExplorer
Set objIE = New InternetExplorer
'make web page visible
objIE.Visible = True
'navigate objIE to this web page
objIE.navigate "https://www.britishhorseracing.com/racing/horses/racehorse-search-results?pagenum=1&q=" & Sheets("Sheet1").Range("A2").Value & "&rated=false"
'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
'You have loaded the result page :)
End Sub

Assign a value from excel to a html input box through excel vba

I have some ISIN-codes from bonds in an excel sheet (column A). I would like to find the corresponding rating from the site https://www.moodys.com.
As this should be done on a regular basis, I would like to automate this process through Excel VBA.
The website does not use any ID's so I can't use getElementById.
I tried using through getElementsByClassName, but this does not work. Also I am not sure if the "search-widget" is the correct class name.
The Error message is:
Object doesn't support this property or method (Error 438)
Sub SearchBot()
Dim objIE As InternetExplorer 'special object variable representing the IE browser
Nbr = ThisWorkbook.Sheets("Sheet1").Cells(1, 1).End(xlDown).Row - 1
With Worksheets("Sheet1").Range(Cells(2, 2), Cells(100000, 6))
.ClearContents
End With
For i = 1 To 1 'Nbr
'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer
'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True
'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "https://www.moodys.com"
'wait here a few seconds while the browser is busy
Application.Wait (Now + TimeValue("00:00:04"))
'Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
Do
DoEvents
Loop Until objIE.readyState = 4
objIE.document.getElementsByClassName("search-widget").Value = Sheets("Sheet1").Range("A" & i + 1).Value
...
The getElementsByClassName method returns an array of all elements with that classname, even if that is only one element. So if you know it is the first and only element with that classname, you can use getElementsByClassName("search-widget")(0).Value
Furthermore, you can find the right classname by inspecting the pages html. In chrome, you can do that by right clicking the element and press "inspect". The only searchbar I saw on the site has "typeahead search-box-input left" as classname by the way.

How to automate a dynamically changing web page url and autofill using Excel VBA?

How to automate a dynamically changing web page url and autofill using Excel VBA?
Your question is a light on the details, but from what I understand your code is working, but you are wanting to grab a new tab that opens up after your code completes.
You can do this with a function I have and use somewhat regularly. This will look at the URLs in your open browser (whether the browser is hidden or not) and allow you to set your object declaration to the tab containing your url.
Warning: Just as JohnRC stated in his comment, automatic manipulation of web pages handling financial transactions is ill-advised. An error in your coding can produce unexpected results. I do not personally recommend automation of financial transactions and any advice you take out of this you do so at your own risk.
Function:
Function GetIE(sLocation As String) As Object
Dim objShell As Object, objShellWindows As Object, o As Object
Dim sURL As String
Dim RetVal As Object
Set RetVal = Nothing
Set objShell = CreateObject("shell.application")
Set objShellWindows = objShell.Windows
For Each o In objShellWindows
sURL = ""
On Error Resume Next
'check the URL and if it's the one you want then
'assign it to the return value and exit the loop
sURL = o.document.Location
On Error GoTo 0
If sURL Like sLocation Then
Set RetVal = o
Exit For
End If
Next o
Set GetIE = RetVal
End Function
In your below code, you are now setting ie2 as the new webpage, with the URL string "*bildesk.com/id*". Once you accomplish this, you can manipulate ie2 just as you did with your first IE object.
Sub SearchBot()
'dimension (declare or set aside memory for) our variables
Dim IE As InternetExplorer 'special object variable representing the IE browser
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
Dim y As Integer 'integer variable we'll use as a counter
Dim result As String 'string variable that will hold our result link
'initiating a new instance of Internet Explorer and asigning it to objIE
Set IE = New InternetExplorer
Dim iedoc As Object
'navigate IE to this web page (a pretty neat search engine really)
IE.navigate "https://www.nbpdcl.co.in/(S(qq5avnlkl4xr2iqstldu0ehl))/frmQuickBillPaymentAll.aspx"
'wait here a few seconds while the browser is busy
Do Until IE.readyState = READYSTATE_COMPLETE: DoEvents: Loop
'in the search box put cell "A2" value
IE.document.getElementById("MainContent_txtCANO").Value = _
Sheets("Sheet1").Range("A2").Value
'click the 'go' button
IE.document.getElementById("MainContent_btnSubmit").Click
'wait here a few seconds while the browser is busy
Do While IE.Busy = True Or IE.readyState <> 4: DoEvents: Loop
'in the amount box put cell "B2" value
IE.document.getElementById("MainContent_txtAmountPayable").Value = _
Sheets("Sheet1").Range("B2").Value
'in the EMAIL box put cell "C2" value
IE.document.getElementById("txtEmailId").Value = _
Sheets("Sheet1").Range("C2").Value
'in the PHONE box put cell "D2" value
IE.document.getElementById("txtMobileNo").Value = _
Sheets("Sheet1").Range("D2").Value
'click the 'verify' button
IE.document.getElementById("MainContent_rbtnlstPaymode_0").Checked = True
'click the 'verify' button
IE.document.getElementById("MainContent_btnConfirmPay").Click
'wait here a few seconds while the browser is busy
Do While IE.Busy = True Or IE.readyState <> 4: DoEvents: Loop
'click the 'verify' button
IE.document.getElementById("MainContent_btnPayNow").Click
'<--------- Use the function here ---------->
Dim ie2 As InternetExplorer
Set ie2 = GetIE("*bildesk.com/id*")
End Sub
So just include the function provided with your module and you should be set.

Excel, VBA and web automation

I have a very big Excel spreadsheet that includes addresses. I want to write a code to automatically open https://www.greatschools.org/, one by one paste addresses in the search box, get the name of best schools for this address, and paste them into the Excel file.
I use this piece of code to paste one of the addresses in the web page but when it pastes and push the search button it doesn't show me any result.
Sub SearchBot()
'dimension (declare or set aside memory for) our variables
Dim objIE As InternetExplorer
'special object variable representing the IE browser
Dim aEle As HTMLLinkElement
'special object variable for an <a> (link) element
Dim y As Integer
'integer variable we'll use as a counter
Dim result As String
'string variable that will hold our result link
Set objIE = New InternetExplorer
objIE.Visible = True
objIE.navigate "https://www.greatschools.org/"
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
objIE.document.getElementsByClassName("form-control")(0).Focus
objIE.document.getElementsByClassName("form-control")(0).Value = _
Sheets("Sheet1").Range("A1").Value
'click the 'go' button
objIE.document.getElementsByClassName("input-group-btn")(0).Click
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
I even added the above line code to give it more time to find result but this doesn't help me at all.
At first, it pastes the address in the search box and after a while the address disappears from the search box and there is no result for me there.
Can anybody help me solving this problem?
Thank you.
These two lines between the Do While loops worked for me:
objIE.Document.getElementsByClassName("form-control")(1).Value = Sheets("Sheet1").Range("A1").Value
'click the 'go' button
objIE.Document.getElementsByClassName("btn search-btn")(0).Click
Notice the use of (1) index on form-control entry.

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