【问题标题】:Select option using Excel Automation with Internet explorer使用带有 Internet Explorer 的 Excel 自动化选择选项
【发布时间】:2019-04-24 14:56:49
【问题描述】:

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

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

 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>

【问题讨论】:

  • 我遇到了类似的问题,结果证明是一段 javascript 处理下拉选择。调用 javascript 是我的解决方案,也许它为您指明了正确的方向?这是我使用的缩短代码:IE.Navigate "javascript:NewItem('\....\u002520Validation\u002520Reports\u002fForms\u002fUpload.aspx?....\u002520Validation\u002520Reports\u0026Type=1')"
  • 网址是公开的吗?
  • @DarXyde:感谢 cmets。任何导致您访问 javascript 的内容,标签信息或标题中是否有任何内容?
  • @QHarr:谢谢,不,网址不公开,它托管在 Intranet 上,是 Remedy 系统的一部分
  • 我要发布一些东西来尝试一下

标签: excel vba internet-explorer web-scraping


【解决方案1】:

您可以尝试将事件附加到选择元素。如果 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

【讨论】:

  • 谢谢我希望这会起作用,因为此代码是新方法。不幸的是,它没有 Dim myHTMLFrame2 As HTMLDocument Set myHTMLFrame2 = HTMLDoc.frames(3).document Dim dropdown As Object, onChange As Object Set dropdown = myHTMLFrame2.querySelector("select[overwrite='1']") Set onChange = myHTMLFrame2. createEvent("htmlevents") onChange.initEvent "change", True, False myHTMLFrame2.document.querySelector("[value='New User(s)']").Selected = True Links.dispatchEvent onChange(这导致了一个对象不支持)
  • 请发布确切的错误消息及其出现的行
  • Dim dropdown As Object, onChange As Object Set dropdown = myHTMLFrame2.querySelector("select[overwrite='1']") Set onChange = myHTMLFrame2.document.createEvent("htmlevents") '文档结果在“对象不支持此属性或方法”中删除了文档,然后没有错误 onChange.initEvent “change”,True,False myHTMLFrame2.querySelector("[value='New User(s)']").Selected = True Links .dispatchEvent onChange 'error "object required" 所以我改成了 myHTMLFrame2.Links.dispatchEvent onChange '上面的错误 "object doesn't support this property or method"
  • 这一行 myHTMLFrame2.querySelector("[value='New User(s)']").Selected = True 产生错误?
  • Set onChange = myHTMLFrame2.document.createEvent("htmlevents") '文档导致错误“对象不支持此属性或方法”删除文档然后没有错误 Set onChange = myHTMLFrame2.createEvent(" htmlevents") Links.dispatchEvent onChange 'error "object required" 所以我改成了 myHTMLFrame2.Links.dispatchEvent onChange '上面的错误 "object doesn't support this property or method"
【解决方案2】:

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

代码:

     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 中的输出:

【讨论】:

  • 谢谢,但没有任何反应 myHTMLFrame2.getElementById("QSGAA5V0GAFRWAOL34D8OK78W2ZUJO").Value = "New User(s)"
  • 我建议您首先检查您的 HTML 文档对象是否已正确分配并且它引用了正确的页面。之后,您可以尝试在文档中查找元素。
【解决方案3】:

@Qharr & @Deepak -- 谢谢

我检查了元素,发现代码在运行时创建了一个“输入”元素。为了执行我使用发送键的事件。不是很优雅,但它有效。

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 "~"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 2020-12-07
    • 2020-06-18
    • 1970-01-01
    • 2012-06-16
    • 2018-04-19
    • 1970-01-01
    相关资源
    最近更新 更多