从vba的下拉框中选择各种选项

时间:2018-08-17 17:15:31

标签: html vba excel-vba web-scraping

如何更改Customer下拉菜单的选项(如下面的HTML示例所示),然后单击“提交”?

我需要将所选值更改为:

Diamond State Generation Partner (Brookside) (LVG- QFTP-RC)  

收件人

Diamond State Generation Partners (Red Lion) (LVG- QFTP-RC)

,然后单击提交。


尝试:

我的尝试如下所示。

在以下行上:

   MyBrowser.document.querySelector("option[value=1745]").Selected = True

产生的错误

  

“运行时错误'-2147352319(80020101)':方法'querySelector'的   对象“ JScriptTypeInfo”失败。


代码:

Sub daily()
    Dim HMTLDoc As HTMLDocument
    Dim MyBrowser As InternetExplorer
    Dim MyHTML_Element As IHTMLElement
    Dim MYURL As String
    '  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 = "username"
    HTMLDoc.all.user_password.Value = "password"
    HTMLDoc.forms(0).submit
    While MyBrowser.Busy Or MyBrowser.readyState < 4: DoEvents: Wend
    ' click reports
    HTMLDoc.getElementsByClassName("menuitem")(1).Click
    While MyBrowser.Busy Or MyBrowser.readyState < 4: DoEvents: Wend
    'click Billing Analysis Report (Industrial)
    HTMLDoc.getElementsByClassName("firstlink")(0).Click
    'click to submit to get to billing analysis report
     With MyBrowser.document
    .querySelector("select[name=vsCustKy]").Click
     'I receive the error message on the next line below
    .querySelector("option[value=1745]").Selected = True
   .querySelector("select[name=vsCustKy]").FireEvent "onchange"
     End With
    HTMLDoc.forms(0).submit

End Sub

完整HTML:

</SCRIPT>
<LINK rel="stylesheet" href="https://www.conectiv.com/cpd/cpd_stylesheet.css">
</HEAD>
<BODY bgcolor="#ffffff" leftmargin="0" marginwidth="0" marginheight="0" topmargin="0" background="https://www.conectiv.com/images/big_c_bg.gif" bgproperties="fixed" vlink="#1f969b" link="#1f969b" onLoad="window.query_form.vsReportMonth.focus();">
<!-- Begin: CPD Navigation Header  -->
<table border="0" cellpadding="0" cellspacing="0">
<tr>
   <td nowrap valign="bottom"><a href="https://www.delmarva.com"><img src="https://webapps.delmarva.com/images/delmarva_logo.png" border="0" align="middle" alt="Delmarva Power"></a></td>
   <td> </td>
   <td> </td>
   <td> </td>
   <td> </td>
   <td> </td>
   <td> </td>
   <td> </td>
   <td> </td>
</tr>
</table>
<!-- End: CPD Navigation Header  -->
<TABLE border="1" cellpadding="6" cellspacing="0" width="100%" bordercolor="#cccccc">
<TR>
<td colspan="1" align="center">
   <table border="0" cellpadding="0" cellspacing="0" width="100%">
      <tr>
         <td align="left" valign="middle" nowrap>
<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>
<BR>
<TABLE WIDTH=500 CELLSPACING=0>
<TR>
<TH VALIGN=TOP ALIGN=LEFT>
<FONT COLOR="BLUE" FACE="Arial, Helvetica" SIZE="4">
<B>INPUT THE MONTH & YEAR :</B>
</FONT>
</TH>
</TR>
</TABLE>
<FORM ACTION="gso_billing_analysis_report" METHOD="POST" NAME="query_form" onSubmit="return verify_entries();">
<INPUT TYPE="hidden" NAME="vsCurrUser" VALUE="2753">
<INPUT TYPE="hidden" NAME="vsTransactionName" VALUE="INTER_BILLING_ANALYSIS_REPORT">
<TABLE WIDTH=500 CELLSPACING=0>
<TR>
<TD>
<TABLE WIDTH=500 BGCOLOR="#FFFFFF" BORDER=1 CELLSPACING=0>
<TR>
<TD ALIGN=LEFT VALIGN=TOP>
<TABLE WIDTH=500 CELLSPACING=0>
<TR>
<TD ALIGN=LEFT COLSPAN=2>
<FONT COLOR="BLUE" FACE="Arial, Helvetica" SIZE="2">
&nbsp;
</FONT>
</TD>
</TR>
<TR>
<TD WIDTH=200 ALIGN=RIGHT VALIGN=CENTER>
<FONT COLOR="BLACK" FACE="Arial, Helvetica" SIZE="2">
<B>MONTH :&nbsp;</B>
</FONT>
</TD>
<TD WIDTH=300 ALIGN=LEFT VALIGN=CENTER>
<SELECT NAME="vsReportMonth">
<OPTION VALUE=1>January
<OPTION VALUE=2>February
<OPTION VALUE=3>March
<OPTION VALUE=4>April
<OPTION VALUE=5>May
<OPTION VALUE=6>June
<OPTION VALUE=7>July
<OPTION SELECTED VALUE=8>August
<OPTION VALUE=9>September
<OPTION VALUE=10>October
<OPTION VALUE=11>November
<OPTION VALUE=12>December
</SELECT>
</TD>
</TR>
<TR>
<TD ALIGN=LEFT COLSPAN=2>
<FONT COLOR="BLUE" FACE="Arial, Helvetica" SIZE="2">
&nbsp;
</FONT>
</TD>
</TR>
<TR>
<TD WIDTH=200 ALIGN=RIGHT VALIGN=CENTER>
<FONT COLOR="BLACK" FACE="Arial, Helvetica" SIZE="2">
<B>YEAR :&nbsp;</B>
</FONT>
</TD>
<TD WIDTH=300 ALIGN=LEFT VALIGN=CENTER>
<FONT COLOR="BLACK" FACE="Arial, Helvetica" SIZE="2">
<INPUT TYPE="TEXT" NAME="vsReportYear" VALUE="2018" SIZE=4 MAXLENGTH=4>
</FONT>
</TD>
</TR>
<TR>
<TD ALIGN=LEFT COLSPAN=2>
<FONT COLOR="BLUE" FACE="Arial, Helvetica" SIZE="2">
&nbsp;
</FONT>
</TD>
</TR>
<TR>
<TD WIDTH=200 ALIGN=RIGHT VALIGN=TOP>
<FONT COLOR="BLACK" FACE="Arial, Helvetica" SIZE="2">
<B>CUSTOMER :&nbsp;</B>
</FONT>
</TD>
<TD WIDTH=300 ALIGN=LEFT VALIGN=TOP>
<FONT COLOR="BLACK" FACE="Arial, Helvetica" SIZE="2">
<SELECT NAME="vsCustKy">
<OPTION SELECTED VALUE=1705>Diamond State Generation Partner (Brookside) (LVG- QFTP-RC)
<OPTION VALUE=1745>Diamond State Generation Partners (Red Lion) (LVG- QFTP-RC)
</SELECT>
</FONT>
</TD>
</TR>
<TR>
<TD ALIGN=LEFT COLSPAN=2>
<FONT COLOR="BLUE" FACE="Arial, Helvetica" SIZE="2">
&nbsp;
</FONT>
</TD>
</TR>
<TR>
<TD ALIGN=CENTER COLSPAN=2>
<INPUT TYPE="submit" VALUE="Continue">
</TD>
</TR>
<TR>
<TD ALIGN=LEFT COLSPAN=2>
<FONT COLOR="BLUE" FACE="Arial, Helvetica" SIZE="2">
&nbsp;
</FONT>
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
</FORM>
</CENTER>
<!-- Begin: CPD Navigation Footer  -->
<tr>
<td colspan="1" align="center">
   <table border="0" width="100%">
      <tr>
         <td align="center"><span class="caption">Copyright: 2018 Delmarva Power. All rights reserved</span></td>
      </tr>
   </table>
</td>
</tr>
</TABLE>
</BODY>
</HTML>

2 个答案:

答案 0 :(得分:1)

该错误是由于缺少用于数字的''引起的。还有一种导航形式。

MyBrowser.document.getElementsByTagName("form")(0).querySelector("option[value='1745']").Selected = True 

在尝试选择之前,您可能需要确保等待。传统上是

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

良好状态之前和任何点击事件之后。

您还可以使用循环来等待元素存在,直到超时。

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

Do
    DoEvents
    On Error Resume Next
    Set ele = MyBrowser.document.getElementsByTagName("form")(0).querySelector("option[value='1745']").
    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.Selected = True

答案 1 :(得分:0)

尝试一下:

Set dropOptions = HTMLDoc.getElementsByTagName("select")
For Each op In dropOptions
    If op.getAttribute("name") = "vsCustKy" Then
        op.Value = 1745
        Exit For
    End If
Next op