VBA Internet Explorer 网页 Link 选择

VBA Internet Explorer Webpage Link Selection

我当前的代码打开网站并登录。下一步是我想在下一页上 select 一个 link 但不知道如何让代码点击它(第一次弄乱 HTML 代码)。下面是 link 后面的 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

根据描述,您似乎想在使用 IE VBA 自动化的页面上单击带有文本 PRC 选择搜索 的 link .

如果我们尝试检查图像中的 HTML 代码,那么我们会注意到该页面包含嵌套表格,而您假设的 link 实际上是 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

输出:

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