首页 > 解决方案 > 使用 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

的HTML: 在此处输入图像描述

<!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="&#10;function submit() {&#10;    [native code]&#10;}&#10;" submit="function WebForm_SaveScrollPositionSubmit() {&#13;&#10;    if (__nonMSDOMBrowser) {&#13;&#10;        theForm.elements['__SCROLLPOSITIONY'].value = window.pageYOffset;&#13;&#10;        theForm.elements['__SCROLLPOSITIONX'].value = window.pageXOffset;&#13;&#10;    }&#13;&#10;    else {&#13;&#10;        theForm.__SCROLLPOSITIONX.value = WebForm_GetScrollX();&#13;&#10;        theForm.__SCROLLPOSITIONY.value = WebForm_GetScrollY();&#13;&#10;    }&#13;&#10;    if ((typeof(this.oldSubmit) != &quot;undefined&quot;) &amp;&amp; (this.oldSubmit != null)) {&#13;&#10;        return this.oldSubmit();&#13;&#10;    }&#13;&#10;    return true;&#13;&#10;}" 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>

^^这是我想与之交互的元素^^**

标签: excelvbaweb-scraping

解决方案


好的,所以找到了解决方案。元素确实隐藏在 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

推荐阅读