【发布时间】:2015-02-20 06:06:48
【问题描述】:
两周以来我一直在尝试自动化网页,但在第三页之后我无法继续进行。
首先,我通过提供凭据登录登录页面,然后单击第二页中的链接。到目前为止,我很好;但在那之后,我需要再次点击我无法点击的第三页的另一个链接,即使我无法阅读该特定页面的正确innerhtml。 innerhtmal 与该页面的源代码不同。使用源代码,我使用了 id/name 来获取元素,但没有用。我看到的问题是 DOCUMENT 对象没有获取第三页的内部细节。当我尝试打印该页面的链接时,它打印了该页面中的一些常见链接,这些链接将在所有页面中可用,而不是打印该特定页面中的所有链接。我想这可能会发生,因为页框相对于 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 暗淡文本作为对象 对于 doc.Links 中的每个文本 Debug.Print text.innerText 下一段文字
在页面的该部分下方,我可以选择输入 FromDate 和 ToDate,通过向此文本框提供日期,我将能够根据日期查看详细信息(默认情况下,页面显示当前第一天的详细信息月到当月的当前日期)。
所以,这里我没有得到链接/或其他详细信息。而且我认为这部分的细节没有存储在 ie.document 对象中。 仅此特定部分的 URL 就与主页不同。
谢谢。
【问题讨论】:
-
每次您导航到新页面时,都会加载一个新文档,因此您需要获取对该新文档的新引用。因此,无论何时退出
Do While ie.Busy循环,都要重新运行Set doc = ie.document行 -
嗨蒂姆,感谢您的回复。顺便说一句,我没有被你的评论清除。即使我在while循环之后尝试使用“Set doc = ie.document”行..但我仍然没有得到任何东西。
-
试试@Jeanno 的解决方案。如果这不起作用,那么您需要发布更多详细信息
-
一个 URL 会有所帮助和/或更多/任何 HTML。尝试仅对此进行调试 = 勉强 minimal reproducible example.