【问题标题】:Select Drop Down from Web Page with IE object使用 IE 对象从网页中选择下拉菜单
【发布时间】:2015-05-29 23:15:41
【问题描述】:

在 VBA 中有以下代码用于下拉选择。

当我运行它时,我在粗体行上看到错误“运行时错误 91 - 对象变量或未设置块变量” ..New to VBA ....

Sub NACDP()
' open IE, navigate to the desired page and loop until fully loaded
    Set ie = CreateObject("InternetExplorer.Application")
    my_url = "https://cdeployna.cognizant.com/"

    With ie
        .Visible = True
        .Navigate my_url
        .Top = 50
        .Left = 530
        .Height = 400
        .Width = 400

    Do Until Not ie.Busy And ie.readyState = 4
        DoEvents
    Loop

    End With

' Input the userid and password
    ie.Document.getElementById("loginControl_UserName").Value = ""
    ie.Document.getElementById("loginControl_Password").Value = ""

' Click the "Login" button
    ie.Document.getElementById("loginControl_LoginButton").Click

    Do Until Not ie.Busy And ie.readyState = 4
        DoEvents
    Loop

    ie.Document.getElementById("ctl00_ddlRoles").selectedindex = 1
    ie.Document.getElementById("ctl00_ddlRoles").FireEvent ("onchange")

     Do Until Not ie.Busy And ie.readyState = 4
        DoEvents
    Loop

    ie.Document.getElementById("ctl00_ContentBody_searchCDPList_ddlFieldName").selectedindex = 1
    ie.Document.getElementById("ctl00_ContentBody_searchCDPList_ddlFieldName").FireEvent ("onchange")
    ie.Document.getElementById("ctl00_ContentBody_searchCDPList_txtValue").Value = "Java"

    ' Click the "Search" button

    ie.Document.getElementById("ctl00_ContentBody_searchCDPList_btnSearch").Click


End Sub

【问题讨论】:

  • 您的粗体已在格式中丢失。请将有问题的代码行复制并粘贴到您的问题中作为编辑。
  • 是哪一行导致上面的错误信息?

标签: vba internet-explorer excel


【解决方案1】:

似乎“直到不是 ie.Busy And ie.readyState = 4”循环并没有真正完成它应该做的事情。收到错误后,如果您按 Debug 然后继续代码,它将成功完成。因此,发生的情况是,当该行首次执行时,您正在搜索的元素仍然不存在。您可以在问题行之前使用以下内容对其进行修补:

Do While ie.Document.getElementById("ctl00_ContentBody_searchCDPList_ddlFieldName") Is Nothing
    DoEvents
Loop

这将循环直到找到元素,然后代码继续成功。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    • 2016-11-28
    • 1970-01-01
    • 2022-11-28
    • 1970-01-01
    • 2017-05-10
    • 2020-02-08
    相关资源
    最近更新 更多