excel - 使用 VBA 代码进行网页抓取:如何浏览登录页面?
问题描述
我是编码新手,正在尝试自动浏览网站。我可以让代码运行并成功自动登录,但是一旦到达下一页,我就无法与任何元素进行交互。在下面的示例中,我希望宏在登录后单击“高级搜索链接”。当我运行代码时,我得到一个“运行时错误 91:对象变量或未设置块变量”。
编码:
Private Sub CommandButton7_Click()
Dim ie As SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Set ie = New InternetExplorerMedium
ie.Visible = True
ie.navigate ("website")
While ie.Busy Or ie.readyState <> 4: DoEvents: Wend
Set HTMLDoc = ie.document
HTMLDoc.all.txtUsername.Value = "username"
HTMLDoc.all.txtPassword.Value = "password"
HTMLDoc.all.imgbtnLogin.Click
While ie.Busy Or ie.readyState <> 4: DoEvents: Wend <<<Code works up to here.
HTMLDoc.getElementById("lnkAdvancedSearch").Click <<<This yields the error messsage.
End Sub
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<style>
.link {
font-family: Arial;
font-size: 8pt;
font-weight: normal;
color: blue;
}
.standard {
font-family: Arial;
font-size: 8pt;
font-weight: normal;
color: black;
}
</style>
</head>
<BODY><FORM onkeypress="javascript:return WebForm_FireDefaultButton(event, 'btnSearch')" id=frmMe method=post name=frmMe action=./todoSummary.aspx oldSubmit=" function submit() { [native code] } " submit="function WebForm_SaveScrollPositionSubmit() { if (__nonMSDOMBrowser) { theForm.elements['__SCROLLPOSITIONY'].value = window.pageYOffset; theForm.elements['__SCROLLPOSITIONX'].value = window.pageXOffset; } else { theForm.__SCROLLPOSITIONX.value = WebForm_GetScrollX(); theForm.__SCROLLPOSITIONY.value = WebForm_GetScrollY(); } if ((typeof(this.oldSubmit) != "undefined") && (this.oldSubmit != null)) { return this.oldSubmit(); } return true; }" oldOnSubmit="null" _events="[object Object]">
<DIV class=standard>
<TABLE width="100%">
<TBODY>
<TR>
<TD>
<TABLE>
<TBODY>
<TR>
**<TD style="VERTICAL-ALIGN: top"><A id=lnkAdvancedSearch class=link href="javascript:__doPostBack('lnkAdvancedSearch','')" shape="">Advanced Search:</A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></DIV></FORM></BODY>
</html>
^^这是我想与之交互的元素^^**
解决方案
好的,所以找到了解决方案。元素确实隐藏在 iframe 中。iframe.document
因此,正如@RaymondWu 在评论中建议的那样,我需要在检索元素之前访问第一个。有关有效的代码,请参见下文。
Private Sub CommandButton7_Click()
'Define variables
Dim ie As SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim iframeDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
'Initialize Internet Explorer and make visible
Set ie = New InternetExplorerMedium
ie.Visible = True
'Navigate to URL
ie.navigate ("website")
'Wait for browser to load page completely
While ie.Busy Or ie.readyState <> 4: DoEvents: Wend
'Get the HTML document for the page
Set HTMLDoc = ie.document
'Input username, password, and login
HTMLDoc.all.txtUsername.Value = "username"
HTMLDoc.all.txtPassword.Value = "password"
HTMLDoc.all.imgbtnLogin.Click
'Wait for browser to load page completely
While ie.Busy Or ie.readyState <> 4: DoEvents: Wend
'Get iframe and check if it exists
Set iframeDoc = HTMLDoc.frames("docs").document
If iframeDoc Is Nothing Then
MsgBox "IFrame was not found."
ie.Quit
Exit Sub
End If
'Get element within iframe and check if it exists
Set HTMLInput = iframeDoc.querySelector("a[id =lnkAdvancedSearch]")
If HTMLInput Is Nothing Then
MsgBox "Element within iframe was not found."
ie.Quit
Exit Sub
Else
'Click element
HTMLInput.Click
End If
End Sub
推荐阅读
- python - 如何在 Paramiko SSH 客户端上增加回滚缓冲区?
- chisel - 你如何测试 RawModules?
- html - 位置固定元素在滚动时移动
- postgresql - Postgresql物化视图有很多刷新效率吗?
- swift - 无法在 macOS Mojave 应用程序中模拟击键
- sql - 从表中查找与确切输入匹配的行
- ruby-on-rails - `initialize':getaddrinfo:名称解析暂时失败(SocketError)
- javascript - 为什么当我对 src URL 进行硬编码时会显示我的 img,而当我以编程方式分配它时却没有?
- git - 不小心从我的电脑上删除了我的项目
- postgresql - PostgreSQL 使用 pg_trgm 比全扫描慢