Unable to autofill tabs of website through excel vba - excel

I have managed to create the below code, but I'm unable to autofill the fields marked with red arrows shown in the image, as there is no ID available for them in the HTML code.
All the tabs shown in image does not have IDs.
What should be the coding for filling these ?
Sub TDS_Autofill()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "https://onlineservices.tin.egov-nsdl.com/etaxnew/tdsnontds.jsp"
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Set doc = IE.document
doc.parentWindow.execScript "sendRequest(281)", "JavaScript"
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
If ThisWorkbook.Sheets("Challan AutoFill").Range("m2").Value = "Company"
Then
doc.getElementById("0020").Click
ElseIf ThisWorkbook.Sheets("Challan AutoFill").Range("m2").Value = "Non
Company" Then
doc.getElementById("0021").Click
End If
If ThisWorkbook.Sheets("Challan AutoFill").Range("o2").Value = "(200)
TDS/TCS Payable by Taxpayer" Then
doc.getElementById("200").Click
ElseIf ThisWorkbook.Sheets("Challan AutoFill").Range("o2").Value = "(400)
TDS/TCS Regular Assessment" Then
doc.getElementById("400").Click
End If
IE.document.querySelector("select.form-control").selectedIndex =
ThisWorkbook.Sheets("Challan AutoFill").Range("r2").Value
doc.getElementById("NetBanking").Click
doc.getElementById("NetBank_Name_c").Value = ThisWorkbook.Sheets("Challan
AutoFill").Range("t2").Value
End Sub```

You could get the inputs using their names by doc.getElementsByName. It returns a collection of all elements in the document with the specified name. Then you should find out the index number of the element you want.
For example, you can set the value of "Tax Deduction Account No" using the following code:
doc.getElementsByName("TAN")(1).Value = "abc"
For the other inputs, you could use the same methods. I make a test with setting some of the values and you can see the result:

You can use getElementsByTag("input") that returns a collection of components that you can access with an index, and then set their value to what you need.
doc.getElementsByTagName("input")(0).value = "My value"

Related

VBA Automation isn't working on new IE tab

It's my first question and I'm new on VBA.
I was trying to run a macro to automatize some procedures on IE. My macro worked very well when opened on diferent windows, but when I tried to run the same macro on tabs it didn't work. It seems (maybe) the macro isn't recognizing the actual tab. Could anyone help me, please?
Sub Test1()
Dim IE As Object
Dim doc As HTMLDocument
Set IE = CreateObject("InternetExplorer.Application")
For intRow = 1 To 3
If intRow = 1 Then
With IE
.Visible = True
.navigate "https://gru.inpi.gov.br/pePI/jsp/patentes/PatenteSearchBasico.jsp"
Do While IE.Busy Or IE.readyState <> 4
Application.Wait DateAdd("s", 1, Now)
Loop
Set doc = IE.document
IE.document.getElementById("principal").Children(4). _
getElementsByTagName("tbody")(0).getElementsByTagName("tr")(5). _
getElementsByTagName("td")(1).getElementsByTagName("font")(0). _
getElementsByTagName("input")(0).Value = "intRow"
Application.Wait DateAdd("s", 2, Now)
End With
Else
With IE
.Visible = True
.navigate "https://gru.inpi.gov.br/pePI/jsp/patentes/PatenteSearchBasico.jsp", 2048&
Do While IE.Busy Or IE.readyState <> 4
Application.Wait DateAdd("s", 1, Now)
Loop
.document.getElementById("principal").Children(4). _
getElementsByTagName("tbody")(0).getElementsByTagName("tr")(5). _
getElementsByTagName("td")(1).getElementsByTagName("font")(0). _
getElementsByTagName("input")(0).Value = "intRow"
Application.Wait DateAdd("s", 2, Now)
End With
End If
Next
End Sub
According to your code and the website, it seems that you want to open multiple tab (with the same URL), and then fill a value in the text box. We should pay attention to the following point:
How to access the web page elements.
How to switch tabs and focus to the new tab.
To access the web page elements, from your web page resource (use F12 developer tools to check it), I notice that the table contains 6 rows, but the last row doesn't contain the input element. So, using your code, I can't find the input element. I found that the input elements contain the class property. So, I suggest you could use the getElementsByClassName method to find the element.
To switch the IE browser tab, we could loop through the opened tabs and compare the URL, and then set focus on the related tab.
More detail steps, please check the following sample code:
Sub Test1()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
Dim intRow As Integer
For intRow = 1 To 3
If intRow = 1 Then
With IE
.Visible = True
.navigate "https://gru.inpi.gov.br/pePI/jsp/patentes/PatenteSearchBasico.jsp"
Do While IE.Busy Or IE.readyState <> 4
Application.Wait DateAdd("s", 1, Now)
Loop
Set doc = IE.document
IE.document.getElementsByClassName("basic")(0).Value = "intRow" & intRow
Application.Wait DateAdd("s", 3, Now)
End With
Else
With IE
.Visible = True
.navigate "https://gru.inpi.gov.br/pePI/jsp/patentes/PatenteSearchBasico.jsp", 2048&
Do While IE.Busy Or IE.readyState <> 4
Application.Wait DateAdd("s", 1, Now)
Loop
Application.Wait DateAdd("s", 3, Now)
Set IE = GetIE("https://gru.inpi.gov.br/pePI/jsp/patentes/PatenteSearchBasico.jsp")
Application.Wait DateAdd("s", 3, Now)
IE.document.getElementsByClassName("basic")(0).Value = "intRow" & intRow
End With
End If
Next
End Sub
Function GetIE(sLocation As String) As Object
Dim retVal As Object
Dim my_url As String, my_title As String
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
'loop through the window and find the tab
For x = 0 To (IE_count - 1)
On Error Resume Next
'get the location and title
my_url = objShell.Windows(x).document.Location
my_title = objShell.Windows(x).document.Title
'debug to check the value
Debug.Print x
Debug.Print my_url
'find the special tab based on the title.
If my_url Like sLocation Then
Set retVal = objShell.Windows(x)
'IE.Quit 'call the Quit method to close the tab.
'Exit For 'exit the for loop
Else
End If
Next
Set GetIE = retVal
End Function
After running the above script, it will open three tabs and fill a value in the Login input text. Please see the following screenshot:
Reference:
Common VBA Methods & Properties used in web automation

Excel VBA automate data entry in a web

Sub Godmode()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "http://charterweb.santanderuk.gs.corp/CharterServiceChain/default.aspx"
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
IE.document.getElementById("M_E_ctlPartField208_V").Value = "test"
End Sub
I am always getting an error saying; method document of object iwebbrowser2 failed
You need to get the element ID, then as a second command assign a value to that element.
elementIwantToChange = IE.document.getElementById("M_E_ctlPartField208_V")
elementIwantToChange.Value = "test"
Also you should test whether the element ID returned is "null"

How do I pause my code until CAPTCHA is entered?

I have managed to compile the below mentioned code, the code is working fine, but I need to pause the code from running until I enter CAPTCHA value and click proceed button.
How can it be achieved ?
I also want the continue the code after I click the proceed button and the next page is loaded.
I have also attached an Image for reference.
Sub TDS_Autofill()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "https://onlineservices.tin.egov-nsdl.com/etaxnew/tdsnontds.jsp"
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Set DOC = IE.document
DOC.parentWindow.execScript "sendRequest(281)", "JavaScript"
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
If ThisWorkbook.Sheets("Challan AutoFill").Range("n2").Value = "Company" Then
DOC.getElementById("0020").Click
ElseIf ThisWorkbook.Sheets("Challan AutoFill").Range("n2").Value = "Non Company" Then
DOC.getElementById("0021").Click
End If
If ThisWorkbook.Sheets("Challan AutoFill").Range("p2").Value = "(200) TDS/TCS Payable by Taxpayer" Then
DOC.getElementById("200").Click
ElseIf ThisWorkbook.Sheets("Challan AutoFill").Range("p2").Value = "(400) TDS/TCS Regular Assessment" Then
DOC.getElementById("400").Click
End If
IE.document.querySelector("select.form-control").selectedIndex = ThisWorkbook.Sheets("Challan AutoFill").Range("s2").Value
DOC.querySelector("#div_nature_error .form-control").FireEvent "onchange"
DOC.getElementById("NetBanking").Click
DOC.getElementById("NetBank_Name_c").Value = ThisWorkbook.Sheets("Challan AutoFill").Range("u2").Value
DOC.querySelector("#NetBank_Name_c").FireEvent "onchange"
DOC.getElementsByName("TAN")(1).Value = ThisWorkbook.Sheets("Challan AutoFill").Range("b2").Value
IE.document.querySelector("select[name=AssessYear]").selectedIndex = ThisWorkbook.Sheets("Challan AutoFill").Range("m2").Value
DOC.getElementsByName("Add_Line1")(1).Value = ThisWorkbook.Sheets("Challan AutoFill").Range("c2").Value
DOC.getElementsByName("Add_Line2")(1).Value = ThisWorkbook.Sheets("Challan AutoFill").Range("d2").Value
DOC.getElementsByName("Add_Line3")(1).Value = ThisWorkbook.Sheets("Challan AutoFill").Range("e2").Value
DOC.getElementsByName("Add_Line4")(1).Value = ThisWorkbook.Sheets("Challan AutoFill").Range("f2").Value
DOC.getElementsByName("Add_Line5")(1).Value = ThisWorkbook.Sheets("Challan AutoFill").Range("g2").Value
IE.document.querySelector("select[name=Add_State]").selectedIndex = 21
DOC.getElementsByName("Add_PIN")(1).Value = ThisWorkbook.Sheets("Challan AutoFill").Range("i2").Value
DOC.getElementsByName("Add_EMAIL")(0).Value = ThisWorkbook.Sheets("Challan AutoFill").Range("j2").Value
DOC.getElementsByName("Add_MOBILE")(0).Value = ThisWorkbook.Sheets("Challan AutoFill").Range("K2").Value
DOC.querySelector("#Submit").Click
End Sub
You could use an input box as this will pause code execution until an answer is submitted and could be used to enter the captcha without having to click on the website. The line below should work:
DOC.getElementsByName("captchaText")(0).Value = InputBox("Captcha:")
If you are on one monitor you will need to open excel on half and the website on the other half so that you can see what the captcha is while you enter it though.

Right Clicking on a particular Web Page in VBA

I am trying to Right click on Google Maps page, So far I am able to create the object ie, navigate to the page and search for a particular address.
But in the next step I have to click on the bottom of Address marker (Blue Dot)
After the click, I want to select What's Here from the options.
After the selection, I use the line
ie.document.getElementsByClassName("link-like widget-reveal-card-lat-lng")(0).innerText to get the co-ordinates of the Place.
Code so Far:
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "https://www.google.com/maps"
Do While ie.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Application.Wait DateAdd("s", 1, Now)
ie.document.getElementById("searchboxinput").Value = "Signature Tower GurGaon"
ie.document.getElementById("searchbox-searchbutton").Click
MsgBox "After Manually clicking What's Here, press ok"
Basically I want to automate that Manual part. What are the elements that needs to be clicked for the right click and how do I identify the point where to click.
Alternate:
Just found that, those lat-long are also part of the a href by
ie.document.getElementsByClassName("gb_9d gb_2 gb_ob")(0).href
So there is no need to right click.
But I am still interested to know how to Right Click it.
Note that you can put the search term directly into the first navigate.
IE.navigate "https://www.google.com/maps" & "/search/Signature Tower GurGaon"
Your wait loop should check .Busy and .ReadyState
Do While IE.Busy Or IE.ReadyState <> 4
And as you already found out the coordinates are in the source code
Option Explicit
Public Sub Test()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "https://www.google.com/maps" & "/search/Signature Tower GurGaon"
Do While IE.Busy Or IE.ReadyState <> 4
Application.Wait DateAdd("s", 1, Now)
Loop
Application.Wait DateAdd("s", 1, Now)
Dim GetLocation As String
GetLocation = IE.document.getElementsByClassName("gb_9d gb_2 gb_ob")(0).href
GetLocation = Right$(GetLocation, Len(GetLocation) - InStr(1, GetLocation, "%40") - 2)
GetLocation = Left$(GetLocation, InStr(1, GetLocation, "&") - 1)
Dim Location() As String
Location = Split(GetLocation, "%2C")
Debug.Print "Lat", Location(0)
Debug.Print "Lon", Location(1)
Debug.Print "zoom", Location(2)
End Sub
Will result in
Lat 28.4636862
Lon 77.054257
zoom 16z

VBA Code for Div Class

I've been searching for an answer for days. Please help.
<div class="FBOSearchResultsItemPricingTablePrice">$3.8391</div>
I just need to pull the "$3.8391" from this string within the website and have it populate into a cell in Excel.
Here's what I have so far, all of which works exactly how I need it to. I have gotten the web page to open, log in and search for a location, however, I can't seem to get this one simple number!!
(I obviously changed the username/password in the code to protect my information)
Sub login()
Dim IE As Object
Dim doc As Object
Set IE = CreateObject("InternetExplorer.application")
IE.AddressBar = 0
IE.StatusBar = 0
IE.Toolbar = 0
IE.Visible = True
IE.navigate "https://www.avfuel.com/login"
Do Until Not IE.busy: DoEvents: Loop
Set doc = IE.document
Do While doc.ReadyState <> "complete": DoEvents: Loop
doc.getElementsByName("dnn$ctr2046$Login$Login_AvfuelAuth$txtUserName")(0).Value = "XXXX"
doc.getElementsByName("dnn$ctr2046$Login$Login_AvfuelAuth$txtPassword")(0).Value = "XXXXX"
doc.getElementById("dnn_ctr2046_Login_Login_AvfuelAuth_cmdLogin").Click
Application.Wait (Now + TimeValue("00:00:05"))
doc.getElementsByName("dnn$ctr618$Loader$ctl00$txtAirportSearchInput")(0).Value = "bfl"
doc.getElementById("dnn_ctr618_Loader_ctl00_hlSearchPageAirport").Click
Application.Wait (Now + TimeValue("00:00:05"))
End Sub
Try this
Cells(1,1) = doc.getElementsByClassName("FBOSearchResultsItemPricingTablePrice")(0).innerText
Or use CSS selector
e.g.
[A1] = doc.querySelector(".FBOSearchResultsItemPricingTablePrice").innerText
The "." is for class.

Resources