【问题标题】:VBA Internet Explorer Webpage Link SelectionVBA Internet Explorer 网页链接选择
【发布时间】:2021-05-21 19:18:15
【问题描述】:

我当前的代码打开一个网站并登录。下一步是我想在下一页选择一个链接,但不知道如何让代码点击它(第一次弄乱 HTML 代码)。下面是该链接后面的 HTML 图片。如果需要更多信息,我可以更新更多图片。

<td align="left" style="vertical-align: top;">
<div class="gwt-Hyperlink">PRC search by selection</div>
</td>

我的 Excel 代码:

Sub Login()
    Dim ie As Object
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True
   ' ie.navigate "http://sqa.subaru-sia.com/nxps/nxps?action=defaultPage"   ' Modify the URL here....
Const Url$ = "http://sqa.subaru-sia.com/nxps/nxps?action=defaultPage"
 Dim UserName As String, Password As String, LoginData As Worksheet
 
    UserName = "xxxxxxx"
    Password = "xxxxxxx"
    
    With ie

    .navigate Url
    ieBusy ie
    .Visible = True

    Dim oLogin As Object, oPassword As Object
    Set oLogin = .document.getElementsByName("j_username")(0)
    Set oPassword = .document.getElementsByName("j_password")(0)

    oLogin.Value = UserName
    oPassword.Value = Password
    .document.forms(0).submit

    End With

    Do While ie.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop
 
   Set myColl = ie.document.getElementsByTagName("div")
    For Each myItm In myColl
        Cells(i + 1, 1) = "Table# " & ti + 1
        ti = ti + 1: i = i + 1
        For Each trtr In myItm.Rows
            For Each tdtd In trtr.Cells
                'Debug.Print (tdtd.innerText)
                If tdtd.innerText = "PRC search by selection" Then
                    tdtd.Children(0).Click
                End If
                j = j + 1
            Next tdtd
            i = i + 1: j = 0
            DoEvents
        Next trtr
        i = i + 1
    Next myItm
    
    'ie.Quit
End Sub


Sub ieBusy(ie As Object)
    Do While ie.Busy Or ie.readyState < 4
        DoEvents
    Loop
End Sub

【问题讨论】:

    标签: html vba internet-explorer hyperlink


    【解决方案1】:

    从描述来看,您似乎想在使用 IE VBA 自动化的页面上单击带有文本PRC search by selection的链接。

    如果我们尝试检查图像中的 HTML 代码,我们会注意到该页面包含嵌套表格,而您假设的链接实际上是一个 DIV 标记。所以我假设你想点击DIV

    我尝试使用嵌套表格和类似的 DIV 制作一个测试网页,并尝试使用以下代码单击 DIV。

    Sub demo()
        Dim ie
        Set ie = CreateObject("InternetExplorer.Application")
        ie.Visible = True
        ie.navigate "https://Your_website_here..."   ' Modify the URL here....
    
        Do While ie.Busy
            Application.Wait DateAdd("s", 1, Now)
        Loop
     
       Set myColl = ie.document.getElementsByTagName("TABLE")
        For Each myItm In myColl
            Cells(i + 1, 1) = "Table# " & ti + 1
            ti = ti + 1: i = i + 1
            For Each trtr In myItm.Rows
                For Each tdtd In trtr.Cells
                    'Debug.Print (tdtd.innerText)
                    If tdtd.innerText = "PRC search by selection" Then
                        tdtd.Children(0).Click
                    End If
                    j = j + 1
                Next tdtd
                i = i + 1: j = 0
                DoEvents
            Next trtr
            i = i + 1
        Next myItm
        
        'ie.Quit
    End Sub
    

    输出:

    此外,您可以尝试根据自己的要求修改代码。

    【讨论】:

    • 我得到运行时错误 '438' 对象不支持此属性或方法。在For each trtr In myItm.Rows 上,我将用完整的代码更新我的问题。代码的第一部分让我通过登录屏幕,进入我需要选择的 div 屏幕。
    • 我不知道你为什么试图在这一行 Set myColl = ie.document.getElementsByTagName("div") 中获取 DIV 而不是 Table。您需要获取所有 Table 元素。此外,在移动到下一页后,尝试检查ie.document 是否正确分配。如果没有,您可以再次尝试分配它。
    • 当我运行代码时,即时窗口显示“正在加载。请稍候...”我认为这是因为代码运行速度超过了 IE 的速度。此外,即使它找到我想要选择的 tdtd.innertext,它也不会选择它。
    • 如果您认为代码执行速度比 IE 快,请尝试使用 Application.Wait DateAdd("s", 1, Now) 等待几秒钟。我建议您按F8 key 逐步调试VBA 代码。它可以帮助缩小问题范围。
    • 从您上次的评论看来,您的问题现在已经解决了。如果该建议有帮助,我建议您接受它作为此问题的答案。它可以在未来帮助其他社区成员解决类似的问题。请参阅here。感谢您的理解。
    猜你喜欢
    • 2013-11-24
    • 2015-04-20
    • 1970-01-01
    • 2017-10-19
    • 2020-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多