从IE中的下拉菜单中选择一个项目

时间:2018-06-20 13:16:43

标签: excel vba web-scraping

在下面的图片中,我尝试从此下拉菜单中单击选项50,该选项在html结构表中实现 任何帮助请 enter image description here

我尝试了这一行

.document.getElementByName("WatchedCompaniesGrid_length").Value = "50"

但这会引发错误

我也尝试过这些线

     Set oElt = ie.document.getElementsByName("WatchedCompaniesGrid_length")
If Not oElt Is Nothing Then
    oElt(0).Value = "50"

End If

它实际上选择了选项,但没有触发事件。.我尝试了oElt.FireEvent“ onchange”,但对我不起作用

这是html行

<input name="WatchedCompaniesGridSelectedId" id="WatchedCompaniesGridSelectedId" type="hidden" value="" autocomplete="off"><script language="javascript" type="text/javascript">WatchedCompaniesGridinit = new RegSysPortal.Grid();WatchedCompaniesGridinit.Initialise('WatchedCompaniesGrid','','','','','','', '', '', 'false', 'true', 'true', 'true', 'two_button', '5', 'asc', '', '10', '1,R,3,C,4,C,5,C,6,C,7,C,8,C', '0', '0', '', '0', null, false, false);</script><div class="frmResponse_blank" id="WatchedCompaniesGrid_result"><span class="frmResponse_message_span"></span></div>    <div class="dataTables_wrapper" id="WatchedCompaniesGrid_wrapper" role="grid"><div class="fg-headerbar"><div class="dataTables_length" id="WatchedCompaniesGrid_length"><span><table><tbody><tr><td>Show </td><td><select name="WatchedCompaniesGrid_length" aria-controls="WatchedCompaniesGrid" size="1"><option value="5">5</option><option value="10">10</option><option value="25">25</option><option value="50">50</option><option value="100">100</option></select></td><td>entries</td></tr></tbody></table></span></div><div class="dataTables_filter" id="WatchedCompaniesGrid_filter"><span><table><tbody><tr><td>Search:</td><td><input aria-controls="WatchedCompaniesGrid" type="text" autocomplete="off"></td></tr></tbody></table></span></div></div><table class="z-index: 2000 dataTable" id="WatchedCompaniesGrid" aria-describedby="WatchedCompaniesGrid_info" style="width: 987px;">

**这是html部分

<table><tbody><tr><td>Show </td><td><select name="WatchedCompaniesGrid_length" aria-controls="WatchedCompaniesGrid" size="1"><option value="5">5</option><option value="10">10</option><option value="25">25</option><option value="50">50</option><option value="100">100</option></select></td><td>entries</td></tr></tbody></table>

2 个答案:

答案 0 :(得分:2)

首先,您正在使用什么版本的IE?原因将取决于此选择的事件触发器是什么。

您可以尝试根据选项值选择该选择的索引。

Set oElt = ie.document.getElementsByName("WatchedCompaniesGrid_length")
    For a = 0 To oElt.Options.Length - 1
        If oElt.Options(a).Text = "sample Option Value" Then
            oElt.selectedIndex = a
            Exit For
        End If
    Next

现在是事件触发部分。选择所需的选项之后。

在IE11中,您可以尝试这个。

Dim objEvent
    Set objEvent = IE.Document.createEvent("HTMLEvents")    'Create an event handler for IE.Document
    objEvent.initEvent "change", False, True
    oElt.dispatchEvent objEvent   

答案 1 :(得分:1)

尝试一下。我使用硬编码的延迟来让浏览器更新其内容。选择器可以从下拉列表中选择和更改所需的数字。但是,唯一的问题是,除非我们选择keyboardevent,否则新内容永远不会更新。

Dim Html As HTMLDocument, post As Object, , evt As Object

Set Html = IE.document ''without this line queryselector fails sometimes

Application.Wait Now + TimeValue("00:00:03")

Set post = Html.querySelector("select[name='WatchedCompaniesGrid_length']")
post.selectedIndex = 3

这是使网页更新结果的部分。我试图说明如何在脚本中使用它。

Set evt = Html.createEvent("keyboardevent")
evt.initEvent "change", True, False

Set post = Html.querySelector("select[name='WatchedCompaniesGrid_length']")
post.selectedIndex = 3
post.dispatchEvent evt