n1xn1x
n1xn1x

Reputation: 27

Paste values into website search bar VBA

I am working on automating a workflow whereby I navigate to a specific website (http://www.boxofficemojo.com/) and paste a value into the website's search bar. I am having trouble interacting with the website search bar in VBA.

the search bar has the following HTML code:

<form name="searchbox" action="/search/q.php" method="POST">
    <input name="q" style="width: 90px; font-weight: bold;" type="text"><br>
    <input type="submit" value="Search...">
</form>

The code command I have right now that doesn't seem to be working is below. I want to paste the excel values from cell c3 into the search bar and then submit.

objIE.Document.getElementByName("q").Value = Sheets("2016").Range("c3").Value

Any ideas?

Upvotes: 0

Views: 3092

Answers (2)

K.Dᴀᴠɪs
K.Dᴀᴠɪs

Reputation: 10139

Tested, this works for me:

Sub Test()

    Dim IE As New InternetExplorer, Doc As HTMLDocument
    Dim oSearch As HTMLDivElement

    With IE
        .Visible = True
        .navigate "http://www.boxofficemojo.com/"
        Do While .Busy Or .readyState <> 4: DoEvents: Loop
        Set Doc = IE.Document
    End With

    Set oSearch = Doc.getElementsByClassName("nl_link")(3)
    Set oSearch = oSearch.getElementsByTagName("input")(0)
    oSearch.Value = "test"

    Doc.forms(0).submit

End Sub

Upvotes: 0

diegorodny
diegorodny

Reputation: 412

objIE.Document.getElementByName("q").Value = Sheets("2016").Range("c3").Value

I believe you want to use "getElementsByName()" which returns a collection of elements. Assuming the first element:

objIE.Document.getElementsByName("q")[0].Value = Sheets("2016").Range("c3").Value

Upvotes: 1

Related Questions