【问题标题】:Select an item from drop down in IE从 IE 的下拉列表中选择一个项目
【发布时间】:2018-06-20 13:16:42
【问题描述】:

在下图中,我试图从这个下拉菜单中单击选项 50,该选项在 html 结构的表格中实现 请提供任何帮助

我试过这条线

.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>

【问题讨论】:

    标签: excel vba web-scraping


    【解决方案1】:

    首先,您使用的是哪个版本的 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   
    

    【讨论】:

    • 非常感谢。这对我帮助很大
    【解决方案2】:

    试试这个。我使用硬编码延迟让浏览器更新它的内容。选择器能够从下拉列表中选择和更改所需的数字。然而,唯一的问题是除非我们选择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
    

    【讨论】:

    • 非常感谢您的回复。我遇到了运行时错误“438”(对象不支持此属性或方法)
    • 您能否使用您粘贴为上图的 html 部分更新您的帖子,以便我可以在本地对其进行测试?该图像不能重复使用。谢谢。
    • 查看编辑。它现在应该可以工作了。我使用.queySelector() 而不是.querySelector()。错过了r,更不用说那里有一个愚蠢的错误。
    • 现在没有错误但是循环是无限的..我该怎么办?
    • 退出循环并设置硬编码延迟,看看会发生什么。你确定那部分不在 iframe 中吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    • 1970-01-01
    • 2017-04-01
    • 2015-07-14
    • 1970-01-01
    相关资源
    最近更新 更多