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

问题描述 投票:1回答:1

有一个网站有一些我想要的工作数据。我登录检查一些复选框并提交,然后该网站将向我发送一封包含我的数据的电子邮件。但是接收我的信息需要花费太长时间,所以我希望自动化它,所以每天早上第一个小时就要求提供信息,就像我在需要时已经将它放在我的电子邮件中一样。

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

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>

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

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

html excel vba web-scraping
1个回答
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)"
© www.soinside.com 2019 - 2024. All rights reserved.