繁体   English   中英

使用Excel Automation和Internet Explorer选择选项

[英]Select option using Excel Automation with Internet explorer

我已经尝试了好几天了。 我可以更新文本框的值并单击选项按钮,但是我无法从下拉列表中选择选项

要选择下拉选项,我尝试过:

 myHTMLFrame2.all.Item("QSGAA5V0GAFRWAOL34D8OK78W2ZUJO").Focus
    myHTMLFrame2.all.Item("QSGAA5V0GAFRWAOL34D8OK78W2ZUJO").selectedIndex = 1
    myHTMLFrame2.all.Item("QSGAA5V0GAFRWAOL34D8OK78W2ZUJO").FireEvent "onchange"

上面什么都没有发生

Dim myHTMLFrame2 As HTMLDocument
    Set myHTMLFrame2 = HTMLDoc.frames(3).document
    Dim elem As Object

    Set elem = myHTMLFrame2.document.getElementById("QSGAA5V0GAFRWAOL34D8OK78W2ZUJO")
(results in "object not supported error")
    elem.Focus
    elem.selectedIndex = 2

这是HTML代码:

<div id="QSGAA5V0GAFRWAOL34D8OK78W2ZUJO-answer-body">
                    <div class="select_holder select_jquery">
                        <select id="QSGAA5V0GAFRWAOL34D8OK78W2ZUJO" overwrite="1" level="0"
                            val="$escapeTool.html($!{answerValue})" required="true" questionid="QSGAA5V0GAFRWAOL34D8OK78W2ZUJO" totalorder="0" questiondefid="QDGAA5V0GAFRWAOL34D8OK78W2ZUJP"
                            responsetype="STATIC_MENU" autocomplete="off"
                            aria-activedescendant="" aria-labelledby="QSGAA5V0GAFRWAOL34D8OK78W2ZUJO-label" tabindex="0">
                            <option value=""></option>
                            <option value="New User(s)" ps="0" aria-selected="false">New User(s)</option>
                            <option value="Modify User Details or Applications" ps="1" aria-selected="false">Modify User Details or Applications</option>
                            <option value="Add/Modify User By Attachment" ps="2" aria-selected="false">Add/Modify User By Attachment</option>
                            <option value="clear">(clear)</option>
                        </select>
                    </div>
                </div>

您可以尝试将事件附加到select元素。 我的目标是使用标签和属性,以防ID为动态。 如果id为static,则使用它。 如果它在iframe中,则在前面添加适当的语法以进行访问。

Dim dropDown As Object, onChange As Object
Set dropDown = ie.document.querySelector("select[overwrite='1']")
Set onChange = ie.document.createEvent("htmlevents")
onChange.initEvent "change", True, False
ie.document.querySelector("[value='New User(s)']").Selected = True
links.dispatchEvent onChange

尝试使用下面的代码进行测试可能有助于解决问题。

码:

     Sub demo()

            Dim URL As String
            Dim IE As Object

            Set IE = CreateObject("InternetExplorer.Application")

            IE.Visible = True

            URL = "C:\Users\Administrator\Desktop\107.html"

            IE.Navigate URL

            Do While IE.ReadyState = 4: DoEvents: Loop   'Do While
            Do Until IE.ReadyState = 4: DoEvents: Loop   'Do Until

             IE.Document.GetElementByID("QSGAA5V0GAFRWAOL34D8OK78W2ZUJO").Value = "Modify User Details or Applications"

            Set IE = Nothing  


End Sub

IE 11中的输出:

在此处输入图片说明

@Qharr&@Deepak-谢谢

我检查了该元素,结果代码在运行时创建了一个“ input”元素。 为了执行事件,我使用发送键。 不是很优雅,但可以。

myHTMLFrame2.all.Item("QSGAA5V0GAFRWAOL34D8OK78W2ZUJO_input").Focus
myHTMLFrame2.all.Item("QSGAA5V0GAFRWAOL34D8OK78W2ZUJO_input").Value = "New User(s)"
myHTMLFrame2.all.Item("QSGAA5V0GAFRWAOL34D8OK78W2ZUJO_input").Click
Application.SendKeys "{DOWN}"
Application.SendKeys "{DOWN}"
Application.SendKeys "~"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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