我正在尝试自动从网站下载文件。到目前为止我尝试过的一些代码如下所示。该页面有四个输入字段(请参阅下面代码中的 url)。
该页面有一个单选按钮,我需要在其中选择 id“RadioChk_2”,它有一个标签:“both”。有 3 个选择框和一个名为“导出到 Excel”的按钮。第一个选择显示了值,例如Chandigarh(O4),存储为值 04。下面的代码尝试填充两个选择框,但不起作用。请帮助或指出如何进一步进展。
提前致谢
Sub WebFormSubmit()
Dim IE As InternetExplorer, URL As String, HTMLDoc As HTMLDocument
Set IE = New InternetExplorer
URL = "http://censusindia.gov.in/2011census/Listofvillagesandtowns.aspx"
IE.navigate URL
IE.Visible = True
Do While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
Set HTMLDoc = IE.document
HTMLDoc.getElementById("drpstate").Value = "Chandigarh(04)"
HTMLDoc.getElementById("drpstate").FireEvent "onchange"
HTMLDoc.getElementById("drpDistrict").Value = "All"
'Wait for the response
Do While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
IE.Quit
End Sub
我尝试了这样的方法,成功了
HTMLDoc.getelementbyid("drpstate").selectedindex = 6
HTMLDoc.getelementbyid("drpstate").FireEvent "onchange"
t = Timer
Do While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE Or Timer - t < 1.5
DoEvents
Loop
HTMLDoc.getelementbyid("drpDistrict").selectedindex = 1
是单身
我无法完全测试,因为我不知道输入框二和三的正确3位和5位代码,但以下内容确实单击了所需的单选按钮,与输入框交互并单击“导出到” Excel”按钮。它至少应该为您进一步的工作提供一些方向。
Sub WebFormSubmit()
Dim IE As InternetExplorer, URL As String, HTMLDoc As HTMLDocument
Set IE = New InternetExplorer
URL = "http://censusindia.gov.in/2011census/Listofvillagesandtowns.aspx"
IE.navigate URL
IE.Visible = True
Do While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
HTMLDoc.getelementbyid("RadioChk_2").Checked = True
HTMLDoc.getelementbyid("drpState").Value = "04"
HTMLDoc.getelementbyid("drpDistrict").Value = "123"
HTMLDoc.getelementbyid("drpsubdistrict").Value = "12345"
HTMLDoc.getelementbyid("btnXls").Click
' Wait for the response
Do While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
IE.Quit
End Sub