Requiring line of code to select nature of payment

2020-05-09 09:54发布

问题:

I complied the below mentiond code line, but it is not working. I'm not able to figure out how to select nature of payment , the nature of payment tab is marked between arrows in the attached image.

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

doc.getElementsByName("NaturePayment").Value = ThisWorkbook.Sheets("Challan 
AutoFill").Range("q2").Value



End Sub

回答1:

You can use selectedIndex on the select itself then specify index of option of interest in the list under that select. I

ie.document.querySelector("select.form-control").SelectedIndex = 2 '3 or 3 etc....

Or

Use .Selected = True on the option element itself e.g.

ie.document.querySelector("[value='193 - Interest on Securities']").Selected = True


回答2:

the getElementsByName method returns an array of elements, even if there is only one element with that name. You have to use getElementsByName("NaturePayment")(0).Value instead.