使用VBA查找并按下网页上的按钮

时间:2016-05-02 01:39:24

标签: vba excel-vba excel

我无法找到搜索按钮的键。我想进入计算机"然后单击搜索按钮。我能够找到输入框的名称,但搜索按钮没有。 我在互联网上找到下面的代码并修改一些变量名称。下面的代码能够导航到bestbuy.com并输入文本" computer"在搜索框上。但是,我坚持编写单击搜索按钮的代码。请帮帮我。

我的vba代码:

Sub searching()
Set a = CreateObject("InternetExplorer.Application")
a.Visible = True
a.navigate ("http://www.bestbuy.com")
Do
DoEvents
Loop Until a.readyState = 4
a.document.all("st").Value = "computer"
a.document.all("search").Click
Do
DoEvents
Loop Until objie.readyState = 4
End Sub

搜索按钮的html代码:



<div class="search-bar" role="search">
<form action="http://www.bestbuy.com/site/searchpage.jsp" name="frmSearch" method="GET">
<label for="gh-search-input">Search Best Buy</label>
<span class="clear-icon"><a href='#clear' class='clear-search-icon'>clear</a> </span>
<input type="text" value="" name="st" maxlength="90" placeholder="Search Best Buy" id="gh-search-input" /><button type="submit" class="search-button" aria-label="Search" title="Search">
<span class="header-icon-search" aria-hidden="true"></span>
</button>
<input type="hidden" value="UTF-8" name="_dyncharset" />
<input type="hidden" value="pcat17071" name="id"/>
<input type="hidden" value="page" name="type" />
<input type="hidden" value="Global" name="sc" />
<input type="hidden" value="1" name="cp" />
<input type="hidden" value="" name="nrp" />
<input type="hidden" value="" name="sp" />
<input type="hidden" value="" name="qp" />
<input type="hidden" value="n" name="list" />
<input type="hidden" value="y" name="iht" />
<input type="hidden" value="All Categories" name="usc" />
<input type="hidden" value="960" name="ks" />
<input id="keys" type="hidden" value="keys" name="keys">
</form>
</div>
&#13;
&#13;
&#13;

感谢

1 个答案:

答案 0 :(得分:0)

解析搜索文本以获取搜索结果

Sub check()
    Dim oHTML_Element As IHTMLElement
    Dim anchorElement As HTMLAnchorElement
    Dim oBrowser As InternetExplorer
    Dim ie As Variant
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True
    searchtext = "Computer" '----Parse your Search Text here
    ie.navigate "http://www.bestbuy.com/site/searchpage.jsp?st=" & searchtext & "&_dyncharset=UTF-8&id=pcat17071&type=page&sc=Global&cp=1&nrp=&sp=&qp=&list=n&iht=y&usc=All+Categories&ks=960&keys=keys"
    While ie.readyState <> READYSTATE_COMPLETE And ie.readyState <> READYSTATE_LOADED
        DoEvents
    Wend
    Application.Wait (Now() + TimeValue("00:00:3"))
End Sub