简体   繁体   中英

VBA HTML Object Internet Explorer automation

How do I select an option from a dropdown menu and update the webpage. Code:

Dim IE As New SHDocVw.InternetExplorer

Dim HTMLDoc As MSHTML.HTMLDocument

then I navigate to a URL and I fetch the dropdown menu using html id and put value=my desired value

HTMLDoc.getElementById("xyz").Value = "dropdown_option" 

I successfully get the option. But, the website doesn't update? eg filling up other fields.

I believe some javascript needs to be run. How can i fix this using VBA?

Source code for dropdown menu field:

<div id="ctl00_MainContent_CreateWebForm__SubjectComboBox_ComboBox" class="Invalid RadComboBox RadComboBox_Telerik SearchComboBox" style="width:206px;white-space:normal;">
    <table summary="combobox" style="border-width:0;border-collapse:collapse;width:100%" class="rcbFocused rcbExpanded">
        <tbody><tr class="rcbReadOnly">
            <td class="rcbInputCell rcbInputCellLeft" style="width:100%;"><input name="ctl00$MainContent$CreateWebForm_$SubjectComboBox$ComboBox" type="text" class="rcbInput radPreventDecorate" id="ctl00_MainContent_CreateWebForm__SubjectComboBox_ComboBox_Input" value="Vælg" readonly="readonly" accesskey="s" autocomplete="off"></td><td class="rcbArrowCell rcbArrowCellRight"><a id="ctl00_MainContent_CreateWebForm__SubjectComboBox_ComboBox_Arrow" style="overflow: hidden;display: block;position: relative;outline: none;">select</a></td>
        </tr>
    </tbody></table><input id="ctl00_MainContent_CreateWebForm__SubjectComboBox_ComboBox_ClientState" name="ctl00_MainContent_CreateWebForm__SubjectComboBox_ComboBox_ClientState" type="hidden" autocomplete="off">
</div>

Source code for dropdown options:

    <div id="ctl00_MainContent_CreateWebForm__SubjectComboBox_ComboBox_DropDown" class="RadComboBoxDropDown RadComboBoxDropDown_Telerik " style="width: 204px; display: block; top: 0px; visibility: visible; transition: none 0s ease 0s;">
        <div class="rcbScroll rcbWidth" style="height: 126px;">
    <ul class="rcbList"><li class="rcbHovered">Forespørgsel til Energinet</li>
    <li class="rcbItem">Måledata</li><li class="rcbItem">Målepunkt eller ad hoc spørgsmål</li>
    <li class="rcbItem">Orientering til alle</li>
    <li class="rcbItem">Priser</li><li class="rcbItem">Pristilknytninger</li></ul></div></div>

For anyone stuck with this issue. I solved this by finding focus property of getelementbyid method. Like this:

  1. First I loop over the list to find my option. I use tagname and classname and matching the innertext property = myoption (This is only necessary if the field doesn't allow you to input text) Then when match occurs, perform .focus followed by .click on that object.

  2. Otherwise just use focus followed by click property on the field and enter your option:

     HTMLDoc.getElementById("xyz").focus HTMLDoc.getElementById("xyz").click HTMLDoc.getElementById("xyz").Value = myoption

This will automatically refresh the html page.

This solved my issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM