How do I pause my code until CAPTCHA is entered? - excel

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.

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

Unable to autofill tabs of website through excel vba

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"

Trying to log in to web page from Excel VBA but username and password grayed out

I'm trying to automate logging in to a web page using VBA code in Excel. I can set the username value and password value, but it appears greyed out and the web page doesn't seem to recognise it. When I send the click event to the login button it says I haven't input the username and password. I've tried setting the focus to the username and password fields first, but no success.
Sub test_login()
Set obj_Shell = CreateObject("Shell.Application")
IE_count = obj_Shell.Windows.count
On Error Resume Next
For x = 0 To (IE_count - 1)
my_url = obj_Shell.Windows(x).Document.Location
' actual url=http://community.betfair.com
If my_url Like "*community*" Then
Set ie = obj_Shell.Windows(x)
Exit For
Else
End If
Next
On Error GoTo 0
Set username_box = ie.Document.getelementbyid("username")
username_box.Value = "myusername"
Set password_box = ie.Document.getelementbyid("passwordText")
password_box.Value = "mypassword"
Set login_button = ie.Document.getelementbyid("login")
login_button.Click
End Sub
The page is expecting text entry via keystrokes which means the dreaded SendKeys is I think required. The following works:
Option Explicit
Public Sub GetInfo()
Dim IE As New InternetExplorer
With IE
.Visible = True
.Navigate2 "http://community.betfair.com/"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
With .getElementById("username")
.Focus
SendKeys "Bob"
End With
SendKeys "{tab}"
Application.Wait Now + TimeSerial(0, 0, 1)
With .getElementById("passwordText")
.Focus
SendKeys "Banana"
End With
Application.Wait Now + TimeSerial(0, 0, 1)
.getElementById("login").Click
While IE.Busy Or IE.readyState < 4: DoEvents: Wend
Stop '<==Delete me later
End With
.Quit
End With
End Sub

Printing Multiple URLs from a Notepad

I have many urls in a Excel File. I am trying to Open Each URL and Print/Save the Page as PDF in silent mode. I have the below code. This works but asking for Print confirmation (Print Dialogue Window). Can anyone has idea to print in silent mode i.e. without showing print dialogue window.
Sub View_Tech_Recalls()
Set ie = CreateObject("InternetExplorer.Application")
Recall_URL = Range("A1").Value
ie.Navigate Recall_URL
ie.StatusBar = False
ie.Toolbar = True
ie.Visible = True
ie.Resizable = True
ie.AddressBar = False
TimeOutWebQuery = 5
TimeOutTime = DateAdd("s", TimeOutWebQuery, Now)
Do Until ie.ReadyState = 4
DoEvents
If Now > TimeOutTime Then
ie.stop
GoTo ErrorTimeOut
End If
Loop
Application.Wait (Now + TimeValue("0:00:03"))
ie.ExecWB 6, 2
ErrorTimeOut:
Set ie = Nothing
End Sub
I'm not sure on implementation for your requirements, but maybe ExportAsFixedFormat Type:=xlTypePDF might be your solution?
Then you can set application.displayalerts = false prior execution, so should go rather silently.

my username and password is missing when submit it using vba for internet explorer

My username and password is missing during submission in this website: https://play.playup.com/
this link address is stored in the one of the cell name E4, can anyone please help me to solve this problem? Please check this code in your editor for better understanding...
username: ajmsanvesh143#gmail.com
password: ajms6237
code:
Sub clicksignupurl()
Dim ie As Object
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
url_name = Sheet1.Range("E4")
If url_name = "" Then Exit Sub
ie.navigate url_name
Do
DoEvents
Loop Until ie.readyState = 4
Set allhyperlinks = ie.document.getElementsByTagName("span")
For Each hyper_link In allhyperlinks
If hyper_link.innertext = "Sign In" Then
hyper_link.Click
Exit For
End If
Next
Set mailid = ie.document.getelementbyid("username")
mailid.Value = "xxxxxxxxx"
Set passid = ie.document.getelementbyid("password")
passid.Value = "xxxxxxx"
Set allhyper = ie.document.getElementsByTagName("button")
For Each hyper_link In allhyper
If hyper_link.innertext = "Sign in" Then
hyper_link.Click
Exit For
End If
Next
End Sub
You're not waiting for the new page to load after the first hyperlink click. Add this line after the Next.
...
Next
do while ie.busy or ie.readystate <> 4: DoEvents: loop 'need this!
Set mailid = ie.document.getelementbyid("username")
...

Resources