使用 Excel VBA 填写网站搜索栏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11785001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:09:31  来源:igfitidea点击:

Fill in website search bar using Excel VBA

htmlformsexcelvbaexcel-vba

提问by user1572431

I'm trying to make an Excel macro that inserts data into a web search form and then copies the results into a table. The web search form is not actually a "form", but a blank table so I can't just change the input value of the form because there is none:

我正在尝试制作一个 Excel 宏,将数据插入到网络搜索表单中,然后将结果复制到表格中。网络搜索表单实际上不是一个“表单”,而是一个空白表格,所以我不能只更改表单的输入值,因为没有:

<td valign="top">
    <table border="0" cellspacing="0" cellpadding="2">
        <tr>
            <th class="navLabelText" align="left">Order:</th>
            <td>
                <input class="navEditField" id="opt_ordernumber_int" name="ordernumber" type="text" size="6" maxlength="6" />
            </td>
        </tr>
    </table>
</td>
<td width="10">&nbsp;</td>

The HTML just continues with more of the same types of forms (I'm guessing coded in Java since the site is a .jsp). Is there any way that I can pass values into the blank table?

HTML 只是继续使用更多相同类型的表单(我猜是用 Java 编码的,因为该站点是一个 .jsp)。有什么方法可以将值传递到空白表中吗?

Here's what I have so far:

这是我到目前为止所拥有的:

Sub featurecode()

Dim ie As Object
Dim doc As HTMLDocument
Dim links As IHTMLElementCollection
Dim link As HTMLAnchorElement
Dim i As Integer
Dim found As Boolean
Dim todaysURL As String

Dim objElement As Object
Dim objCollection As Object

Set ie = CreateObject("InternetExplorer.Application")

ie.Visible = True 'false
ie.navigate "https://website.com"
Application.StatusBar = "Loading Feature Codes"

Do Until ie.readyState = IE_READYSTATE.complete: DoEvents: Loop

Set doc = ie.document


' Find the input tag of the order form and submit button:
Set objCollection = ie.document.getElementsByTagName("input")

i = 0
While i < objCollection.Length
    If objCollection(i).Name = "ordernumber" Then

        ' Set text for search
        objCollection(i).Value = "655032"

    Else
        If objCollection(i).Type = "submit" And objCollection(i).Name = "Submit" Then

            ' "Search" button is found
            Set objElement = objCollection(i)
            objElement.Click

        End If
    End If
    i = i + 1
Wend

End Sub

The part that I'm having trouble with is this:

我遇到问题的部分是:

If objCollection(i).Name = "ordernumber" Then

    ' Set text for search
    objCollection(i).Value = "655032"

Usually you can change the HTML value of the form, but in this case there is no HTML value in the input tag, so I'm at a loss. My goal here is to simply insert an order number into the form and hit the submit button. Unfortunately I can't show you the website as it's an internal corporate site, but here's a screenshot of the relevant info: screenshot

通常你可以改变表单的HTML值,但是在这种情况下,输入标签中没有HTML值,所以我不知所措。我的目标是简单地在表单中插入一个订单号并点击提交按钮。很遗憾,我无法向您展示该网站,因为它是公司内部网站,但这是相关信息的屏幕截图screenshot

Thanks!

谢谢!

回答by zygomatic

I've found with some elements in VBA, including INPUT, you have to focus on the element first:

我发现 VBA 中有一些元素,包括 INPUT,你必须首先关注元素:

    objCollection(i).Focus
    objCollection(i).Value = "655032"