如何滚动和选择/双击列表框项目?

时间:2019-02-12 13:21:36

标签: excel vba web-scraping listboxitem

我正在尝试使用VBA-Excel获取Intranet网络报告。如何滚动到列表中的项目并选择或双击

我使用其父元素的ID及其自身的类和内部文本值来定位该项目。我尝试了很多方法 .click 方法,但没有成功。

Sub VBA_IE()

    Dim IE As New SHDocVw.InternetExplorerMedium
    Dim HTMLDoc As MSHTML.HTMLDocument
    Dim HTMLDiv As MSHTML.IHTMLElement
    Dim HTMLDivs As MSHTML.IHTMLElementCollection

    '
    'Here is IE object set and the page loaded
    '

    Set HTMLDoc = IE.document
    Set HTMLDivs = HTMLDoc.getElementById "id_mstr104ListContainer").getElementsByTagName("div")
    For Each HTMLDiv In HTMLDivs
        If HTMLDiv.getAttribute("title") = "Fourth Center" Then
            HTMLDiv.Focus
            HTMLDiv.click
            'wait-routines
            Application.Wait Now + TimeValue("00:00:05")
            Exit For
         Else
         End If
    Next HTMLDiv

    IE.Quit
    Set IE = Nothing

End Sub

页面来源的一部分(于2019年2月20日延长):

<div style="display: block;" class="mstrPromptQuestion" id="id_mstr80">
    <span class="">
        <div class="mstrPromptQuestionTitleBar">
            <table cellspacing="0" cellpadding="0">
                <tbody>
                    <tr>
                        <td align="left" valign="middle">
                            <span class="mstrPromptQuestionTitleBarIndex" style="display: inline;">1.</span>
                            <span class="mstrPromptQuestionTitleBarTitle">Delivery Source</span>
                            <span class="mstrPromptQuestionTitleBarRequired" title="(Required)">(Required)</span>
                        </td>
                        <td width="1%" align="right" valign="middle">
                            <img src="../images/1ptrans.gif" align="absmiddle" style="display: none" class="mstrPromptQuestionBackToTopImg" title="Back to top" onclick="mstr.behaviors.PromptQuestion.onClickBackToTop('id_mstr80')">
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
        <table cellspacing="0" cellpadding="0" class="mstrPromptQuestionInfoTable">
            <tbody>
                <tr>
                    <td class="mstrPromptQuestionInfoCellLeft">
                        <div class="mstrPromptQuestionMeaning" style="display: block;">Select items for Delivery Source</div>
                        <div class="mstrPromptQuestionInstructions" style="display: none;"></div>
                        <div class="mstrPromptQuestionMessage" style="display: none;"></div>
                    </td>
                    <td class="mstrPromptQuestionInfoCellRight">
                        <span class="mstrPlaceholder" name="saveAnswerView" id="id_mstr81"></span>
                    </td>
                </tr>
            </tbody>
        </table>
        <div class="mstrPromptQuestionContents">
            <table cellspacing="0" cellpadding="0" class="mstrPromptQuestionSimpleAnswerViewTitle" style="display: none;">
                <tbody>
                    <tr>
                        <td class="mstrPromptQuestionSimpleAnswerViewTitleCellRadio">
                            <input value="0" name="id_mstr80_radio" onmousedown="mstr.behaviors.PromptQuestion.onClickRadio('id_mstr80', false)" id="id_mstr80_radio_simpleAnswerView" type="radio" checked="true">
                        </td>
                        <td class="mstrPromptQuestionSimpleAnswerViewTitleCellText">
                            <label for="id_mstr80_radio_simpleAnswerView">Your selection:</label>
                        </td>
                    </tr>
                </tbody>
            </table>
            <div style="padding-left: 0px;" class="mstrPromptQuestionSimpleAnswerView" onmousedown="this.previousSibling.rows[0].cells[0].childNodes[0].checked = true; mstr.behaviors.PromptQuestion.onClickRadio('id_mstr80', false)">
                <table cellspacing="0" style="display: table;" cellpadding="0" class="mstrListCart" id="id_mstr84">
                    <colgroup><col class="mstrListCartLeftCol"><col class="mstrListCartButtonCol"><col class="mstrListCartRightCol"><col class="mstrListCartButtonCol"></colgroup>
                    <tbody>
                        <tr>
                            <td class="mstrListCartRowPathView" style="width:100%;" colspan="4">
                                <table cellspacing="0" cellpadding="0" class="mstrListCartTablePathView" style="display: none;">
                                    <tbody>
                                        <tr>
                                            <td class="mstrListCartCellPathView">
                                                <span class="mstrPlaceholder" name="pathView" id="id_mstr87"></span>
                                            </td>
                                            <td class="mstrListCartCellPathUpButton">
                                                <span class="mstrPlaceholder" name="pathUpButton" id="id_mstr88"></span>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </td>
                        </tr>
                        <tr>
                            <td class="mstrListCartCellSearchView" style="width:100%;" colspan="4">
                                <div title="" class="mstrSearchField" id="id_mstr99" style="display: block;">
                                    <table cellspacing="0" cellpadding="0" class="mstrSearchFieldTable">
                                        <tbody>
                                            <tr>
                                                <td class="mstrSearchFieldSearchBox">
                                                    <div title="" class="mstrTextBoxWithIcon" id="id_mstr102" style="display: block;">
                                                        <div class="mstrTextBoxWithIconCaption">
                                                            <label for="id_mstr102_txt">Search for:</label>
                                                        </div>
                                                        <table cellspacing="0" cellpadding="0" class="mstrTextBoxWithIconTable">
                                                            <tbody>
                                                                <tr>
                                                                    <td class="mstrTextBoxWithIconCellInput">
                                                                        <div>
                                                                            <input maxlength="" onclick="if (mstr.utils.ISW3C) {this.focus();}" onkeypress="return mstr.behaviors.TextBoxWithIcon.onkeypress(arguments[0], self, 'id_mstr102', this.value)" name="id_mstr102_txt" style="background-color: rgb(255, 255, 255);" id="id_mstr102_txt" size="" type="text">
                                                                        </div>
                                                                    </td>
                                                                    <td class="mstrTextBoxWithIconCellIcon">
                                                                        <div class="mstrToolButtonRoundedRight">
                                                                            <input type="button" title="Search" onmouseout="mstr.behaviors.ToolButtonRounded.unhover(this.parentNode)" style="background-position: left center; cursor: pointer;" class="mstrBGIcon_tbSearch" src="../images/1ptrans.gif" onmouseover="mstr.behaviors.ToolButtonRounded.hover(this.parentNode)" onclick="mstr.behaviors.TextBoxWithIcon.fire('id_mstr102', this.parentNode.parentNode.previousSibling.childNodes[0].childNodes[0].value);return false;">
                                                                        </div>
                                                                    </td>
                                                                </tr>
                                                            </tbody>
                                                        </table>
                                                    </div>
                                                </td>
                                                <td class="mstrSearchFieldMatchCaseBox">
                                                    <div class="mstrCheckBox" style="display: block;" id="id_mstr103">
                                                        <input type="checkbox" id="id_mstr103_input" onclick="mstr.$obj('id_mstr103').getModel() &amp;&amp; mstr.$obj('id_mstr103').getModel().set('searchMatchCase', this.checked); mstr.$obj('id_mstr103').fireCommands(null, this.checked);">
                                                        <label title="" for="id_mstr103_input">Match case</label>
                                                    </div>
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td class="mstrListCartCellAvailableView">
                                <table cellspacing="0" cellpadding="0" style="table-layout: auto; width: 100%;">
                                    <tbody>
                                        <tr>
                                            <td></td>
                                        </tr>
                                    </tbody>
                                </table>
                                <table cellspacing="0" cellpadding="0" title="" class="mstrListBlock" id="id_mstr104" style="display: table; width: 100%;">
                                    <tbody>
                                        <tr>
                                            <td class="mstrListBlockCell">
                                                <span class="">
                                                    <div class="mstrListBlockCaption" style="display: block;">Available:</div>
                                                    <div class="mstrListBlockHeader" style="display: none">
                                                        <span class="mstrPlaceholder" name="headerView" id="id_mstr105"></span>
                                                    </div>
                                                    <div style="width: auto; height: 150px;" class="mstrListBlockContents" id="ListBlockContents_id_mstr104">
                                                        <div oncontextmenu="return mstr.behaviors.Generic.oncontextmenu(arguments[0], self, 'id_mstr104');" onmouseup="try{mstr.$obj('id_mstr104').focus();}catch(localerr){};  return mstr.behaviors.Generic.clearBrowserHighlights(self)" onmousedown="var retVal = mstr.behaviors.ListView.onmousedown(arguments[0], self, 'id_mstr104'); try{mstr.$obj('id_mstr104').focus();}catch(localerr){}; return retVal" ondblclick="return mstr.behaviors.ListView.ondblclick(arguments[0], self, 'id_mstr104')" class="mstrListBlockListContainer" id="id_mstr104ListContainer" style="display: block; height: 80px;">
                                                            <div class="mstrListBlockItemSelected" title="First Center">
                                                                <div class="mstrBGIcon_ae mstrListBlockItemName" style="background-position: 2px 50%; padding-left: 23px;">First</div>
                                                            </div>
                                                            <div class="mstrListBlockItem" title="Second Center">
                                                                <div class="mstrBGIcon_ae mstrListBlockItemName" style="background-position: 2px 50%; padding-left: 23px;">Second&nbsp;Center</div>
                                                            </div>
                                                            <div class="mstrListBlockItem" title="Third Added Center">
                                                                <div class="mstrBGIcon_ae mstrListBlockItemName" style="background-position: 2px 50%; padding-left: 23px;">Third&nbsp;Added&nbsp;Center</div>
                                                            </div>
                                                            <div class="mstrListBlockItem" title="Fourth Center">
                                                                <div class="mstrBGIcon_ae mstrListBlockItemName" style="background-position: 2px 50%; padding-left: 23px;">Fourth&nbsp;Center</div>
                                                            </div>
                                                        </div>
                                                        <div class="mstrListBlockReadyState" style="display: none;"></div>
                                                        <div class="mstrListBlockMessageSection" style="display: none;">
                                                            <div class="mstrListBlockMessage"></div>
                                                        </div>
                                                    </div>
                                                    <a title="" href="#" onkeydown="return mstr.$obj('id_mstr104').onkeydown(arguments[0], self);" onkeypress="return mstr.$obj('id_mstr104').onkeypress(arguments[0], self);" class="mstrListBlockTabStop" id="id_mstr104TabStop">
                                                        <div></div>
                                                    </a>
                                                </span>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </td>
                            <td class="mstrListCartCellAddRemoveButtons">
                                <div>
                                    <div class="mstrToolButtonRounded" id="id_mstr106" style="display: block;">
                                        <img title="Add" onmouseout="mstr.behaviors.ToolButtonRounded.unhover(this.parentNode)" style="background-position: left center; cursor: pointer;" class="mstrBGIcon_tbAdd" src="../images/1ptrans.gif" onmouseover="mstr.behaviors.ToolButtonRounded.hover(this.parentNode)" align="absmiddle" onclick="var v = mstr.$obj('id_mstr106'); return (v.get('enabled')) ? mstr.$obj('id_mstr106').fireCommands() : false;">
                                    </div>
                                </div>
                                <div>
                                    <div class="mstrToolButtonRounded" id="id_mstr107" style="display: block;">
                                        <img title="Add All" onmouseout="mstr.behaviors.ToolButtonRounded.unhover(this.parentNode)" style="background-position: left center; cursor: pointer;" class="mstrBGIcon_tbAddAll" src="../images/1ptrans.gif" onmouseover="mstr.behaviors.ToolButtonRounded.hover(this.parentNode)" align="absmiddle" onclick="var v = mstr.$obj('id_mstr107'); return (v.get('enabled')) ? mstr.$obj('id_mstr107').fireCommands() : false;">
                                    </div>
                                </div>
                                <div style="height: 11px; width: 1px; overflow: hidden"></div>
                                <div>
                                    <div class="mstrToolButtonRounded" id="id_mstr108" style="display: block; background-position: left center;">
                                        <img title="Remove" onmouseout="mstr.behaviors.ToolButtonRounded.unhover(this.parentNode)" style="background-position: right center; cursor: default;" class="mstrBGIcon_tbRemove" src="../images/1ptrans.gif" onmouseover="mstr.behaviors.ToolButtonRounded.hover(this.parentNode)" align="absmiddle" onclick="var v = mstr.$obj('id_mstr108'); return (v.get('enabled')) ? mstr.$obj('id_mstr108').fireCommands() : false;">
                                    </div>
                                </div>
                                <div>
                                    <div class="mstrToolButtonRounded" id="id_mstr109" style="display: block; background-position: left center;">
                                        <img title="Remove All" onmouseout="mstr.behaviors.ToolButtonRounded.unhover(this.parentNode)" style="background-position: right center; cursor: default;" class="mstrBGIcon_tbRemoveAll" src="../images/1ptrans.gif" onmouseover="mstr.behaviors.ToolButtonRounded.hover(this.parentNode)" align="absmiddle" onclick="var v = mstr.$obj('id_mstr109'); return (v.get('enabled')) ? mstr.$obj('id_mstr109').fireCommands() : false;">
                                    </div>
                                </div>
                            </td>
                            <td class="mstrListCartCellSelectedView">
                                <table cellspacing="0" cellpadding="0" style="table-layout: auto; width: 100%;">
                                    <tbody>
                                        <tr>
                                            <td>
                                                <table cellspacing="0" cellpadding="0" title="" class="mstrListBlock" id="id_mstr110" style="display: table; width: 100%;">
                                                    <tbody>
                                                        <tr>
                                                            <td class="mstrListBlockCell"></td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                                <span class="">
                                    <div class="mstrListBlockCaption" style="display: block;">Selected:</div>
                                    <div class="mstrListBlockHeader" style="display: none">
                                        <span class="mstrPlaceholder" name="headerView" id="id_mstr111"></span>
                                    </div>
                                    <div style="width: auto; height: 150px;" class="mstrListBlockContents" id="ListBlockContents_id_mstr110">
                                        <div oncontextmenu="return mstr.behaviors.Generic.oncontextmenu(arguments[0], self, 'id_mstr110');" onmouseup="try{mstr.$obj('id_mstr110').focus();}catch(localerr){};  return mstr.behaviors.Generic.clearBrowserHighlights(self)" onmousedown="var retVal = mstr.behaviors.ListView.onmousedown(arguments[0], self, 'id_mstr110'); try{mstr.$obj('id_mstr110').focus();}catch(localerr){}; return retVal" ondblclick="return mstr.behaviors.ListView.ondblclick(arguments[0], self, 'id_mstr110')" class="mstrListBlockListContainer" id="id_mstr110ListContainer" style="display: none; height: auto;"></div>
                                        <div class="mstrListBlockReadyState" style="display: none;"></div>
                                        <div class="mstrListBlockMessageSection" style="display: block;">
                                            <div class="mstrListBlockMessage">(none)</div>
                                        </div>
                                    </div>
                                    <a title="" href="#" onkeydown="return mstr.$obj('id_mstr110').onkeydown(arguments[0], self);" onkeypress="return mstr.$obj('id_mstr110').onkeypress(arguments[0], self);" class="mstrListBlockTabStop" id="id_mstr110TabStop">
                                        <div></div>
                                    </a>
                                </span>
                            </td>
                            <td class="mstrListCartCellUpDownButtons">
                                <span class="mstrPlaceholder" name="upButton" id="id_mstr96"></span>
                                <span class="mstrPlaceholder" name="downButton" id="id_mstr97"></span>
                            </td>
                        </tr>
                        <tr>
                            <td class="mstrListCartCellFetchView" colspan="4">
                                <table title="" cellspacing="0" style="display: table;" cellpadding="0" class="mstrFetchControl" id="id_mstr112">
                                    <tbody>
                                        <tr>
                                            <td>
                                                <img title="First page" src="../images/1ptrans.gif" align="absmiddle" onclick="mstr.behaviors.FetchControl.fireFetchCommand('id_mstr112', 'FetchFirst')" class="mstrBGIcon_fetchFirstDisabled " style="display: none;">
                                            </td>
                                            <td>
                                                <img title="Previous Page" src="../images/1ptrans.gif" align="absmiddle" onclick="mstr.behaviors.FetchControl.fireFetchCommand('id_mstr112', 'FetchPrevious')" class="mstrBGIcon_fetchPreviousDisabled " style="display: none;">
                                            </td>
                                            <td class="mstrFetchControlCellText">
                                                <div onmouseout="this.className = this.className.replace(/Hover/,'')" onmousedown="mstr.behaviors.FetchControl.on_mousedown('id_mstr112')" class="mstrFetchControlText" onmouseover="if (!this.disabled) this.className += 'Hover'">1 - 4 of 4</div>
                                            </td>
                                            <td>
                                                <img title="Next page" src="../images/1ptrans.gif" align="absmiddle" onclick="mstr.behaviors.FetchControl.fireFetchCommand('id_mstr112', 'FetchNext')" class="mstrBGIcon_fetchNextDisabled " style="display: none;">
                                            </td>
                                            <td>
                                                <img title="Last page" src="../images/1ptrans.gif" align="absmiddle" onclick="mstr.behaviors.FetchControl.fireFetchCommand('id_mstr112', 'FetchLast')" class="mstrBGIcon_fetchLastDisabled " style="display: none;">
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <table cellspacing="0" cellpadding="0" class="mstrPromptQuestionComplexAnswerViewTitle" style="display: none;">
                <tbody>
                    <tr>
                        <td class="mstrPromptQuestionComplexAnswerViewTitleCellRadio">
                            <input value="1" name="id_mstr80_radio" onmousedown="mstr.behaviors.PromptQuestion.onClickRadio('id_mstr80', true)" id="id_mstr80_radio_complexAnswerView" type="radio">
                        </td>
                        <td class="mstrPromptQuestionComplexAnswerViewTitleCellText">
                            <label for="id_mstr80_radio_complexAnswerView">The default selection is:</label>
                        </td>
                    </tr>
                </tbody>
            </table>
            <div style="padding-left: 23px; display: none;" class="mstrPromptQuestionComplexAnswerView" onmousedown="this.previousSibling.rows[0].cells[0].childNodes[0].checked = true;               mstr.behaviors.PromptQuestion.onClickRadio('id_mstr80', true)">
                <div title="" class="mstrTextDiv" style="display: block;" id="id_mstr113">
                    <div class="mstrTextDivCaption"></div>
                    <div class="mstrTextDivValue"></div>
                </div>
            </div>
        </div>
    </span>
</div>

VBA代码只是像没有发生任何事情一样通过命令行,或者报告该方法不适用于该元素的错误。

1 个答案:

答案 0 :(得分:0)

您可能需要触发事件,但这是您可以尝试的两件事

ie.document.parentWindow.execScript "document.querySelector('[title^=Fourth]').setAttribute('class','mstrListBlockItemSelected');"

ie.document.querySelector("[title='Fourth Center']").FireEvent "ondblclick"