刮痧下拉菜单选择

时间:2018-03-23 09:45:59

标签: excel vba excel-vba

您好我有一个使用VBA自动化的代码,到目前为止我设法达到的是代码打开网址,输入凭据,访问搜索页面,但是在搜索页面中,我不能选择下拉菜单中的任何选项。

下面是网站上的代码和VBA代码

Sub Test()

Dim rng As Range
Set rng = Sheets("sheet1").Range("A1", Sheets("sheet1").Cells.Range("A1").End(xlDown))
Dim ie As Object


    Set ie = CreateObject("InternetExplorer.application")
    ie.Visible = True
    ie.Navigate ("Home page")
    Do
        If ie.ReadyState = 4 Then

            Exit Do
        Else
            DoEvents
        End If
    Loop

    ie.Document.forms(0).all("txtUsername").Value = ""
    ie.Document.forms(0).all("txtPassword").Value = ""
    ie.Document.forms(0).submit
    ie.Visible = True

    Application.Wait (Now + TimeValue("00:00:02"))

For Each cell In rng

    ie.Navigate ("search URL")

    DoEvents

   Set myElementsCollection = ie.Document.getElementsByTagName("Global Service Reference")

    ie.Document.getElementById("txtFieldValue").Select

    SendKeys (cell.Value)

    DoEvents

    ie.Document.forms(0).getElementById("cmdFind").Click

Next cell

End Sub

以及网页的代码作为其内部网URL

<form action="search.asp?Find=1" method="post" onsubmit="return validate(this);">
<table border="0" cellspacing="0" cellpadding="2" width="100%">
    <tbody><tr>
        <td valign="top">
            <br><table border="0" cellspacing="0" cellpadding="2" width="100%">
                <tbody><tr>
                    <td colspan="3"><b><font size="3" face="cambria" color="#e60000">Please enter your search criteria:</font></b></td>
                </tr>
                <tr>
                    <td width="20%">
                        <select name="cboFieldName">
                            <option value="0"></option>
                            <option value="1">Customer Name</option>
                            <option value="2">Customer Reference</option>
                            <option value="3">Site Name</option>
                            <option value="4">Site City</option>
                            <option value="5">Site Country</option>
                            <option value="6">Global Service Reference</option>
                            <option value="7">Customer Service Reference</option>
                        </select>
                    </td>
                    <td align="center" width="10%"><font size="2" face="Tahoma" color="#000000">contains</font></td>
                    <td width="50%">
                        <input type="text" name="txtFieldValue" value="">
                    </td>
                </tr>
            </tbody></table>
        </td>
        <td width="25%" valign="top">
            <table border="0" cellpadding="0" cellspacing="0">
                <tbody><tr>
                    <td width="2" bgcolor="#003399">&nbsp;</td>
                    <td width="10">&nbsp;</td>
                    <td>
                        <font size="2" face="Tahoma">
                        <input type="submit" style="background-color:#a3418f" name="cmdFind" value="Find">
                        <p>
                        <a href="login/welcome.asp" style="color:#666666" img="" src="../icons/doclink1.gif" border="0" align="center" width="19" height="19">All Customers</a><br><br>
                        <a href="search.asp" style="color:#666666" img="" src="../icons/doclink1.gif" border="0" align="center" width="19" height="19">New Search</a><br><br>
                        <a href="javascript:ShowHelp()" target="_self" style="color:#666666" img="" src="../icons/doclink1.gif" border="0" align="center" width="19" height="19">Help...</a>
                    </p></font></td>
                </tr>
            </tbody></table>
        </td>
    </tr>
</tbody></table>
</form>

1 个答案:

答案 0 :(得分:0)

试试这个。它应该工作:

Sub Select_dropdown_item()
    With CreateObject("InternetExplorer.Application")
        .Visible = True
        .navigate "you_link"
        While .Busy = True Or .readyState < 4: DoEvents: Wend
        ''if the script fails, activate the delay
'        Application.Wait Now + TimeValue("00:00:05")

'        Set post = .document.getElementsByName("cboFieldName")(0)''activate this line if you want to select using index
'        post.selectedIndex = 2

        ''the below portion will select the item using it's name
        For Each post In .document.getElementsByName("cboFieldName")(0).getElementsByTagName("option")
            If InStr(post.innerText, "Customer Name") > 0 Then post.Selected = True: Exit For
        Next post
    End With
End Sub