Excel VBA问题:IE自动化期间未显示列表

时间:2014-01-21 13:50:40

标签: excel internet-explorer vba excel-vba automation

我正在尝试构建一个简单的工具,填写3字段表单并使用excel vba下载相应的文件

让我用我的代码解释我面临的问题:

代码如下(如果复制粘贴在excel中,则无误):

Sub ScripHistoryDownloadertry10()

Dim URL As String, Scripcode As String
Dim ie As Object, ieDoc As Object

Dim StartDate As String, EndDate As String


StartDate = "01/01/1990"    'Day(#1/1/1990#) & "/" & Month(#1/1/1990#) & "/" & Year(#1/1/1990#)   ' #1/1/1990#
EndDate = "20/01/2014"      'Day(Date) & "/" & Month(Date) & "/" & Year(Date)
URL = "http://www.bseindia.com/markets/equity/EQReports/StockPrcHistori.aspx?expandable=7&flag=0"

'Scripcode = "500010"
'I 'll be putting a for loop here to loop through about 5000 values of Scripcode once this issue is solved.

Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True
    ie.navigate URL

Do Until (ie.readyState = 4 And Not ie.Busy)
    DoEvents
Loop

Set ieDoc = ie.document

'This line below is my problem ===============================================================

ieDoc.getElementById("ctl00_ContentPlaceHolder1_GetQuote1_txtscrip_code").Value = 500010     'Scripcode

'==========================================================================================

ieDoc.getElementById("ctl00_ContentPlaceHolder1_txtFromDate").Value = StartDate
ieDoc.getElementById("ctl00_ContentPlaceHolder1_txtToDate").Value = EndDate
ieDoc.getElementById("ctl00_ContentPlaceHolder1_btnSubmit").Click

Do Until (ie.readyState = 4 And Not ie.Busy)
    DoEvents
Loop

'================= Below need to be uncommented to download the file ===========
'ieDoc.getElementById("ctl00_ContentPlaceHolder1_btnDownload").Click
'I'll be inducing a code here to download the file later.
'ie.Quit
'=============================================================================

Set ie = Nothing
Set ieDoc = Nothing

End Sub

我已经设法完全按照预期构建代码而没有任何错误。问题是,当我们处理URL页面手动并填写表单中的第一个字段(我在代码中标记为问题的那个)时,会出现一个小列表,具体取决于我手动输入的内容。 但每当我尝试使用excel vba IE自动化为该字段分配值时,下拉列表不会出现。任何人都可以帮助如何使列表出现并选择列表中最顶端的值?

Here a screenshot how the list looks when filling data manually:

1 个答案:

答案 0 :(得分:0)

解决方案是为我改变方法。

等了很长时间才找到解决方案后,我终于找到了解决问题的方法.. :) 首先我安装了Selenium Wrapper Library,它帮助我们用excel vba控制chrome浏览器,然后我根据库提供的功能重写了代码...与上面的代码相同,采用了不同的方法:) 这也帮助我解决了通过Internet Explorer下载文件的问题,其中一个问题是在IE中点击下载按钮提示... 可能这个代码对某些人有帮助.. :))

以下是代码:

Sub ScripHistoryDownloaderv2()

Dim selDriver As Object
Dim URL As String, Scripcode As String
Dim StartDate As String, EndDate As String

Scripcodez = "500010"
' Set up a loop over here


StartDate = "01/01/1990"
'EndDate = "20/01/2014"
URL = "http://www.bseindia.com/markets/equity/EQReports/StockPrcHistori.aspx?expandable=7&scripcode=" & Scripcodez & "&flag=sp&Submit=G"


dd = Application.ActiveWorkbook.Path



Set selDriver = CreateObject("SeleniumWrapper.WebDriver")
selDriver.Start "chrome", "http://www.google.com/"
selDriver.Open URL

selDriver.Type "id=ctl00_ContentPlaceHolder1_txtFromDate", StartDate
'selDriver.Type "id=ctl00_ContentPlaceHolder1_txtToDate", EndDate
selDriver.findElementById("ctl00_ContentPlaceHolder1_btnSubmit").Click
selDriver.Click "id=ctl00_ContentPlaceHolder1_btnSubmit"
selDriver.clickAndWait "ctl00_ContentPlaceHolder1_btnDownload"
selDriver.Wait 9000

selDriver.stop


End Sub