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

标签 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>

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

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

最佳答案

尝试

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)"

关于html - 使用 Excel VBA 单击网站中的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55404789/

相关文章:

javascript - 为什么 (navigator.devicememory) 对于内存超过 6gb 的设备不准确?

javascript - 使用 VBA 从 iFrame 填充网页上的下拉列表

vba:删除过滤的行但不是第一个(或删除后存储并粘贴)

vba - 将范围放入数组以获得更有效的循环

vba - 迄今为止不更改列的格式

ms-access - Microsoft Access 2007 和 2010 : "Run-Time Error ' 42 9': ActiveX component can' t create object"

html - 对齐 Bootstrap 按钮内的文本

html - 嵌套 SVG 的对齐方式

css - slider 在 IE8 中不起作用

vba - 如何使用 vba 将单元格值作为图表标题