首页 文章

如何使用Excel VBA自动化动态更改的网页?

提问于
浏览
0

自从两周以来我一直在尝试自动化网页,但是在第3页后我无法继续进行 .

首先我'm logging into login page by giving credentials and then I would click a link from 2nd page. Until this point I'很好;但在那之后我又需要点击第3页的另一个链接,我无法,甚至我无法读取该特定页面的正确 innerhtml . innerhtmal 与该页面的源代码不同 . 使用源代码我已经使用id / name来获取元素但没有用 . 问题我'm seeing is the DOCUMENT object is not taking the inner details of 3rd page. When I tried to print the links of that page it printed me some common links in that page which would be available in all the pages instead of printing all the links in that particular page. I guess this might happen because the page frame varies with respect to the FromDate & ToDate. Pardon me if I'错了 . 我们是否需要每次更改"ie.document"对象相对于网页导航?因为我认为第一次加载页面时它坚持相同 .

以下是我的代码:

Public Sub Test ()

    Dim shellWins As ShellWindows
    Dim ie As InternetExplorer
    Dim doc As HTMLDocument
    Dim frm As HTMLFrameElement
    Dim frms As HTMLElementCollection
    Dim strSQL As String
    Dim Login As Boolean

    strSQL = "https://website.com"   

    Set ie = CreateObject("InternetExplorer.Application")
    With ie
        .Visible = True
        .Navigate strSQL

        Do Until .ReadyState = 4: DoEvents: Loop
            Set doc = ie.document

            Dim link As Object

        For Each link In doc.Links
            'Debug.Print link.innerText
             If link.innerText = "Click Here" Then
                link.Click
                Exit For
             End If
        Next link                   

           Do While ie.Busy: DoEvents: Loop

Login_Pane:

For Each link In doc.Links
       If link.innerText = "Leave & Attendance" Then
           'Debug.Print doc.body.innerHTML
           link.Click
           Login = True
          Exit For
         End If
    Next link

      If Login <> True Then
        Application.Wait (Now + TimeValue("00:00:02"))
        Application.SendKeys "<USERNAME>", True
        Application.SendKeys "{TAB}"
        Application.Wait (Now + TimeValue("00:00:02"))
        Application.SendKeys "<PASSWORD>", True
        Application.SendKeys "{ENTER}"
        GoTo Login_Pane
       End If

     Do While ie.Busy: DoEvents: Loop

      Dim link As Object

    For Each link In doc.Links
        Debug.Print link.innerText 

        ' Above line code should print all the links in that page_
          _but unfortunatly it is not displaying as it is in the source code.
        ' instead printing half of the links which are commonly_ _available in all pages. 
        ' This page has three frames

    Next link


      End With
      'IE.Quit
End Sub

我无法发布该页面的图像以使您了解更多,无论如何我会尽我所能 .

当我使用下面的代码时,我只能从页面的上半部分获取链接 .

设置doc = ie.document Dim text As Object For Each text in doc.Links Debug.Print text.innerText Next text

在页面的这一部分下面,我可以选择输入FromDate和ToDate,通过给这个文本框提供日期,我将能够根据日期查看详细信息(默认页面显示从第一个月到详细信息的详细信息)当月的当前日期) .

所以,在这里我没有得到链接/或其他细节 . 我认为这部分的细节不存储在ie.document对象中 . 仅此特定部分与主页面具有不同的URL .

谢谢 .

1 回答

  • 0

    几点想法:

    • 对于动态加载的页面,您需要使用 Application.Wait (5秒左右)而不是 Do Until .ReadyState = 4: DoEvents: Loop . 如果你正在执行javascript,后者不起作用 .

    • 应始终避免使用SendKeys,因为它不健壮 . 使用DOM资源管理器检查元素以获取ID或名称 .

相关问题