使用 VBA 导航网站并修改下拉框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21583561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use VBA to navigate a website and modify drop down box
提问by John W
Good Morning,
早上好,
I am working a VBA project where I would like to access an internal company, login required, web site and extract data from a table. I have successfully written code that will navigate to the correct website, and then modify a drop down box value from "All Transactions" to "Running Record". The problem I have is that I cannot not get the data in the table to refresh once the drop down box is updated. On the website, next to the drop down field, is a "Go" hyperlink. Whenever I try to activate the link, using vba, it resets the drop down and I don't get the data displayed that I want.
我正在工作一个 VBA 项目,我想访问内部公司、需要登录、网站并从表中提取数据。我已经成功编写了可以导航到正确网站的代码,然后将下拉框的值从“所有交易”修改为“运行记录”。我遇到的问题是,一旦下拉框更新,我就无法刷新表中的数据。在网站上,下拉字段旁边是一个“Go”超链接。每当我尝试使用 vba 激活链接时,它都会重置下拉列表,并且我没有显示我想要的数据。
I was able to find a similar post where a user explained that the issue was related to the site being secured and that a .FireEvent would need to be used. Although the concept made sense to me implementation was another story. The biggest difference between my situation and the other's was that when his drop down box was changed it would activate the event. In my case there is a seperate ID, the "Go" hyperlink I mentioned above, that needs to be activated once the drop down value is entered.
我找到了一个类似的帖子,其中用户解释说该问题与受保护的站点有关,并且需要使用 .FireEvent。虽然这个概念对我来说很有意义,但实施是另一回事。我和其他人最大的区别是,当他的下拉框改变时,它会激活事件。就我而言,有一个单独的 ID,即我上面提到的“Go”超链接,一旦输入下拉值,就需要激活它。
Below is a portion of the HTML code where drop down box options are located as well as the "GO" hyperlink.
下面是 HTML 代码的一部分,其中包含下拉框选项以及“GO”超链接。
<select name="classList" onchange="selectClass(this);"><option class=tblrow2 value="All" selected=selected>All Schedules</option>
<option value="A" >
Transactions-Financial
</option>
<option value="N" >
Transactions-Non-Financial
</option>
<option value="RB" >
Running Balance
</option></select>
<input type="hidden" name="classindx" id = "classindx" value="" />
<a href="#" onClick="submitTransForm()"> Go </a>
This is a portion of my code that modifies the dropdown box value.
这是我修改下拉框值的代码的一部分。
'try to get form by ID
Set frm = ie.Document.getElementByID("myFormID")
'try to get form by name
If frm Is Nothing Then Set frm = ie.Document.getElementsByName("facilityFeeRepayForm").Item(0)
'Set ieButton = ie.Document.getElementById("classindx") Here I tried to treat the Go hyperlink like a button but that didn't work either.
If frm Is Nothing Then
Else
ie.Visible = True
For Each element In frm.elements
On Error Resume Next
Select Case element.Name
Case "classList": element.Value = "RB"
ie.getElementByID("classindx").FireEvent("onClick") 'nothing happens with this code
While ie.ReadyState <> 4: DoEvents: Wend
End Select
Next
End If
I would appreciate any help you can offer.
如果您能提供任何帮助,我将不胜感激。
Thank you, John
谢谢你,约翰
回答by Tim Williams
Instead of clicking the link, try this:
不要点击链接,试试这个:
ie.Document.parentWindow.execScript "submitTransForm()", "JavaScript"