vba有多个按钮时单击一个按钮

时间:2018-08-30 21:07:46

标签: html excel vba excel-vba web-scraping

我需要以下问题的帮助: -点击“前一天”按钮

我当前拥有的代码将登录到一个网站,单击交易链接,然后单击提交按钮。我被困在需要单击标记为“前一天”的按钮的位置。我包含了有问题的html,我尝试使用HTMLDoc.getElementsByClassName(“ gso_nominate_maint”)(1)。单击尝试并单击上一个按钮,但收到错误消息。

Dim HMTLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Sub transactions()
'
' transactions Macro
'

 Dim MyHTML_Element As IHTMLElement
    Dim MYURL As String
    ' Delmarva website
    MYURL = "website"
    Set MyBrowser = New InternetExplorer
    MyBrowser.Silent = True
    MyBrowser.navigate MYURL
    MyBrowser.Visible = True
    Do
    Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
    Set HTMLDoc = MyBrowser.document
    ' user login and password
    HTMLDoc.all.user_login.value = "user"
    HTMLDoc.all.user_password.value = "pass"
    ' click submit
    HTMLDoc.forms(0).submit
    While MyBrowser.Busy Or MyBrowser.readyState < 4: DoEvents: Wend
    ' click transactions
    HTMLDoc.getElementsByClassName("menuitem")(0).Click
    While MyBrowser.Busy Or MyBrowser.readyState < 4: DoEvents: Wend
    'click Billing Analysis Report (Industrial)
    HTMLDoc.getElementsByClassName("firstlink")(1).Click
    While MyBrowser.Busy Or MyBrowser.readyState < 4: DoEvents: Wend
    HTMLDoc.forms(0).submit
    HTMLDoc.forms(0).submit
    'currently get an error from the code line below. 
    HTMLDoc.getElementsByClassName("gso_nominate_maint")(1).Click
    While MyBrowser.Busy Or MyBrowser.readyState < 4: DoEvents: Wend
    End Sub

<a href="gso_etrader_main?vsCurrUser=2753" class="menuitem">Trader Home</a><span class="body"> | </span>
<a href="gso_list_etrader_transaction?vsCurrUser=2753" class="menuitem">Transaction</a><span class="body"> | </span>
<a href="gso_list_etrader_reports?vsCurrUser=2753" class="menuitem">Reports</a>
         </td>
      </tr>
   </table>
</td>
</TR>
<TR>
<TD>
<CENTER>
<TABLE WIDTH=750 BORDER=0 ALIGN=CENTER>
<TR>
<TD ALIGN=RIGHT VALIGN=BOTTOM>
<FORM ACTION="gso_nominate_maint" METHOD="POST">
<INPUT TYPE="hidden" NAME="vsCurrUser" VALUE="2753">
<INPUT TYPE="hidden" NAME="vsTransactionName" VALUE="INTER_NOMINATE_ENTRY">
<INPUT TYPE="hidden" NAME="vsDlyOrderKy" VALUE="13052">
<INPUT TYPE="submit" VALUE="Previous Day">
</FORM>
</TD>
<TD ALIGN=CENTER>
<BR>
<B><FONT SIZE="+2">N</FONT><FONT SIZE="+1">OMINATE</FONT></B>&nbsp;&nbsp;
<B><FONT SIZE="+2">O</FONT><FONT SIZE="+1">RDERS</FONT></B>&nbsp;&nbsp;
<B><FONT SIZE="+2">F</FONT><FONT SIZE="+1">OR</FONT></B>&nbsp;&nbsp;
<B><FONT SIZE="+2">Thursday  08/30/2018</FONT></B>
<BR>
<FONT FACE="ARIAL, HELVETICA" SIZE="-1">
<B>[ Maintenance ]</B>
</FONT>
</TD>
<TD ALIGN=LEFT VALIGN=BOTTOM>
<FORM ACTION="gso_nominate_maint" METHOD="POST">
<INPUT TYPE="hidden" NAME="vsCurrUser" VALUE="2753">
<INPUT TYPE="hidden" NAME="vsTransactionName" VALUE="INTER_NOMINATE_ENTRY">
<INPUT TYPE="hidden" NAME="vsDlyOrderKy" VALUE="13054">
<INPUT TYPE="submit" VALUE="Next Day">
</FORM>
</TD>
</TR>
</TABLE>
<BR>
<BR>
<TABLE WIDTH=750 BORDER=0>
<TR>
<TD WIDTH=450 ALIGN=RIGHT>&nbsp;
</TD>
<TD ALIGN=RIGHT WIDTH=50>
<FORM ACTION="gso_nominate_ff_header" METHOD="POST">
<INPUT TYPE="hidden" NAME="vsOrderHdrKy" VALUE="">
<INPUT TYPE="hidden" NAME="vsDlyOrderKy" VALUE="13053">
<INPUT TYPE="hidden" NAME="vsTransactionName" VALUE="INTER_NOMINATE_ENTRY">
<INPUT TYPE="hidden" NAME="vsCurrUser" VALUE="2753">
<INPUT TYPE="submit" VALUE="Add New">
</FORM>
</TD>
</TR>
</TABLE>
<TABLE WIDTH=750 BORDER=0>
<TR>
<TH WIDTH=250 NOWRAP VALIGN=BOTTOM ALIGN=CENTER>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">
SERVICE
</FONT>
</TH>
<TH WIDTH=50 NOWRAP VALIGN=BOTTOM VALIGN=CENTER>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">
RANK
</FONT>
</TH>
<TH WIDTH=75 NOWRAP VALIGN=BOTTOM VALIGN=CENTER>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">
NOMINATED<BR>VOLUME
</FONT>
</TH>
<TH WIDTH=75 NOWRAP VALIGN=BOTTOM VALIGN=CENTER>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">
CONFIRMED<BR>VOLUME
</FONT>
</TH>
<TH WIDTH=150 NOWRAP VALIGN=BOTTOM VALIGN=CENTER>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">
COMMENTS
</FONT>
</TH>
<TH WIDTH=75 NOWRAP VALIGN=BOTTOM VALIGN=CENTER>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">
NOMINATED<BR>TOTAL
</FONT>
</TH>
<TH WIDTH=75 NOWRAP VALIGN=BOTTOM VALIGN=CENTER>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">
CONFIRMED<BR>TOTAL
</FONT>
</TH>
</TR>
<TR>
<TD NOWRAP WIDTH=250 ALIGN=LEFT VALIGN=TOP>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">
<B><A HREF="gso_nominate_ff_header?vsOrderHdrKy=239887&vsDlyOrderKy=13053&vsTransactionName=INTER_NOMINATE_ENTRY&vsCurrUser=2753">Direct Sale Columbia</A></B>
</FONT>
</TD>
<TD NOWRAP WIDTH=50 ALIGN=CENTER VALIGN=TOP>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">
&nbsp;
</FONT>
</TD>
<TD NOWRAP WIDTH=75 ALIGN=RIGHT VALIGN=TOP>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">
           0
</FONT>
</TD>
<TD NOWRAP WIDTH=75 ALIGN=RIGHT VALIGN=TOP>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">
           0
</FONT>
</TD>
<TD NOWRAP WIDTH=150 ALIGN=LEFT VALIGN=TOP>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">
&nbsp;
</FONT>
</TD>
<TD NOWRAP WIDTH=75 ALIGN=RIGHT VALIGN=TOP>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">
           0
</FONT>
</TD>
<TD NOWRAP WIDTH=75 ALIGN=RIGHT VALIGN=TOP>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">
           0
</FONT>
</TD>
</TR>
<TR>
<TD NOWRAP WIDTH=250 ALIGN=LEFT VALIGN=TOP>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">
<B><A HREF="gso_nominate_ff_header?vsOrderHdrKy=239917&vsDlyOrderKy=13053&vsTransactionName=INTER_NOMINATE_ENTRY&vsCurrUser=2753">Direct Sale ESNG</A></B>
</FONT>
</TD>
<TD NOWRAP WIDTH=50 ALIGN=CENTER VALIGN=TOP>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">
&nbsp;
</FONT>
</TD>
<TD NOWRAP WIDTH=75 ALIGN=RIGHT VALIGN=TOP>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">
           0
</FONT>
</TD>
<TD NOWRAP WIDTH=75 ALIGN=RIGHT VALIGN=TOP>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">
           0
</FONT>
</TD>
<TD NOWRAP WIDTH=150 ALIGN=LEFT VALIGN=TOP>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">
&nbsp;
</FONT>
</TD>
<TD NOWRAP WIDTH=75 ALIGN=RIGHT VALIGN=TOP>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">
           0
</FONT>
</TD>
<TD NOWRAP WIDTH=75 ALIGN=RIGHT VALIGN=TOP>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">
           0
</FONT>
</TD>
</TR>
<TR>
<TD NOWRAP WIDTH=250 ALIGN=LEFT VALIGN=TOP>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">
<B><A HREF="gso_nominate_ff_header?vsOrderHdrKy=239947&vsDlyOrderKy=13053&vsTransactionName=INTER_NOMINATE_ENTRY&vsCurrUser=2753">Direct Sale Transco</A></B>
</FONT>
</TD>
<TD NOWRAP WIDTH=50 ALIGN=CENTER VALIGN=TOP>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">
&nbsp;
</FONT>
</TD>
<TD NOWRAP WIDTH=75 ALIGN=RIGHT VALIGN=TOP>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">
           0
</FONT>
</TD>
<TD NOWRAP WIDTH=75 ALIGN=RIGHT VALIGN=TOP>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">
           0
</FONT>
</TD>
<TD NOWRAP WIDTH=150 ALIGN=LEFT VALIGN=TOP>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">
&nbsp;
</FONT>
</TD>
<TD NOWRAP WIDTH=75 ALIGN=RIGHT VALIGN=TOP>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">
           0
</FONT>
</TD>
<TD NOWRAP WIDTH=75 ALIGN=RIGHT VALIGN=TOP>
<FONT FACE="ARIAL, HELVETICA" SIZE="-2">

1 个答案:

答案 0 :(得分:1)

在完全基于提供的HTML之前,有一个form元素需要导航才能到达该按钮。然后,您可以使用CSS属性选择器通过元素的属性value和值'Previous Day'

来定位元素
MyBrowser.document.getElementsByTagName("form")(0).querySelector("[value='Previous Day']").Click

这假定没有要导航的其他form / frame / iframe标签。

您可以在此处看到父表单:

enter image description here

请记住,必须有一个

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

允许之前加载页面。

您可能需要额外的等待时间,例如定时循环,以使该元素可用,例如

Dim t As Date, ele As Object
Const WAIT_TIME_SECS As Long = 5
t = Timer

Do
    DoEvents
    On Error Resume Next
    Set ele = MyBrowser.document.getElementsByTagName("form")(0).querySelector("[value='Previous Day']")
    On Error GoTo 0
    If Timer - t > WAIT_TIME_SECS Then Exit Do
Loop While ele Is Nothing

If Not ele Is Nothing Then ele.Click