簡體   English   中英

如何使用Excel VBA自動化動態更改的網頁?

[英]How to automate a dynamically changing web page using Excel VBA?

自從兩周以來我一直試圖自動化網頁,但是在第3頁后我無法繼續進行。

首先我通過提供憑據登錄登錄頁面,然后點擊第2頁的鏈接。 在此之前我很好; 但在那之后我又需要點擊第3頁中我無法找到的另一個鏈接,即使我無法讀取該特定頁面的正確內部innerhtml innerhtmal與該頁面的源代碼不同。 使用源代碼我已經使用id / name來獲取元素但沒有用。 我看到的問題是DOCUMENT對象沒有采取第3頁的內部細節。 當我嘗試打印該頁面的鏈接時,它在該頁面中打印了一些常用鏈接,這些鏈接將在所有頁面中可用,而不是打印該特定頁面中的所有鏈接。 我想這可能會發生,因為頁面框架因FromDate和ToDate而異。 如果我錯了,請原諒我。 我們是否需要每次更改“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. 對於動態加載的頁面,您需要使用Application.Wait (5秒左右)而不是Do Until .ReadyState = 4: DoEvents: Loop 如果你正在執行javascript,后者不起作用。
  2. 應始終避免使用SendKeys,因為它不健壯。使用DOM資源管理器檢查元素以獲取ID或名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM