使用Excel VBA在网站中单击复选框

时间:2019-03-28 18:42:57

标签: html excel vba web-scraping

有一个网站,其中包含我想要的一些工作数据。我登录选中某些复选框并提交,然后该网站将向我发送包含我的数据的电子邮件。但是接收我的信息花费的时间太长,所以我想每天自动在第一天的第一个小时对它进行请求,这样我就可以在需要时将其保存在电子邮件中。

我修改了一些代码,我已经设法登录并进入需要单击复选框(其中有三个)的页面,然后单击“提交”按钮。

Sub GetTable()

Dim ieApp As InternetExplorer
Dim ieDoc As Object
Dim ieTable As Object
Dim chkBox As Object

'create a new instance of ie
Set ieApp = New InternetExplorer

'you don’t need this, but it’s good for debugging
ieApp.Visible = True

'assume we’re not logged in and just go directly to the login page
ieApp.Navigate "http://mydata/aspx/mydataLogon.aspx?Language=2"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

Set ieDoc = ieApp.Document

'fill in the login form – View Source from your browser to get the control names
With ieDoc.forms(0)
    .txtUsername.Value = "usertest"
    .txtPassword.Value = "test123"
    .Submit
End With
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

'now that we’re in, go to the page we want
ieApp.Navigate "http://mydata/InventoryTracking/aspx/rptDefault.aspx?rpt=../aspx/invRptDetailReport.aspx"

Set ieDoc = ieApp.Document

With ieDoc

    'I tried with getElementbyId, I dont get any error but the chkbox still comes as with a nothing value
    Set chkBox = ieDoc.getElementByid("cblPlants_0")

    'therefore these give error too, I dont know why. A an object variable or with block variable not set error
    chkBox.Click
    chkBox.Checked = True

    'here I get an object variable or with block variable not set error
    ieDoc.getElementByid("cblPlants_0").Click

    .Submit
End With

End sub

我想要的复选框控件位于其中:

'first there is a form and this is how the iframe is first called I think
<form name="_ctl1" method="post" action="rptDefault.aspx" id="_ctl1">
    <table width="100%" HEIGHT="100%" cellpadding="0" cellspacing="0" border="0">  
        <TR>
            <TD CLASS="Normal" COLSPAN="4"><IFRAME ID="IFRAME1" NAME="IFRAME1" SRC="../aspx/invRptDetailReport.aspx" frameborder="0" width="100%" height="100%"></IFRAME></TD>
        </TR>  

'Now the iframe1 goes like this

<form name="frmRptDetailReport" method="POST" action="invRptDetailReport.aspx" id="frmRptDetailReport">  
    <table id="htblMainBody" width="100%" cellpadding="0" cellspacing="0" border="0" bgcolor="#E5DBE2">  

        <tr><td <a id="lbtnSelect" href="javascript:__doPostBack(&#39;lbtnSelect&#39;,&#39;&#39;)"</a>  
        <div id="divChkboxlist" class="scrollingControlContainer scrollingCheckBoxList" onscroll="saveScrollPos();">  
             <table id="cblPlants" border="0" style="font-family:Arial;font-size:11px;width:275px;overflow: scroll"><tr>  
                    <tr><td><input id="cblPlants_0" type="checkbox" name="cblPlants:0" onclick="javascript:setTimeout(&#39;__doPostBack(\&#39;cblPlants$0\&#39;,\&#39;\&#39;)&#39;, 0)" language="javascript" /><label for="cblPlants_0">1X1 -Confecciones</label></td>
    </tr>

如何在此页面上选中此复选框?选中此复选框后,我将收到我想要的电子邮件。

如果您需要更多信息,请告诉我

1 个答案:

答案 0 :(得分:0)

尝试

ieDoc.querySelector("#cblPlants_0").click

等同于:

ieDoc.getElementById("cblPlants_0").click

在.Navigate步骤之后,请记住要有适当的页面加载,然后再尝试点击

While ieApp.Busy Or ieApp.readyState < 4: DoEvents: Wend

Set ieDoc = ieApp.Document

ieDoc.getElementById("cblPlants_0").click

在点击之前,您可能需要额外的等待。逐步执行F8进行测试。

您还可以尝试执行javascript

ieDoc.parentWindow.execScript "javascript:setTimeout('__doPostBack(\'cblPlants$0\',\'\')', 0)"