如何在Excel中使用VBA放置焦点并点击提交按钮?

问题描述 投票:1回答:1

我是一个新的网络搜索和VBA的新手,但我渴望进一步学习.我有一个小的项目,并慢慢地一步一步来.我有一个Excel模块中的一小段代码,几乎是工作。我只是不能让它提交我的搜索框文本.我基本上在我的Excel工作表的A2单元格里有例子马的名字。这段代码打开了网站,也将文本输入到搜索框中,我只是无法让它点击 "Go "按钮。我希望得到任何帮助,并解释我做错了什么,这样我就可以避免再次出现这种情况。

当检查网站上的Go按钮时,HTML是这样的。

我的代码

Sub HorseSearch()

    'define objIE
    Dim objIE As InternetExplorer
    'create an instance objIE
    Set objIE = New InternetExplorer
    'make web page visible
    objIE.Visible = True

    'navigate objIE to this web page
    objIE.navigate "https://www.britishhorseracing.com/racing/horses/racehorse-search-results/"

    'wait here a few seconds while the browser is busy
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

    'in the search box put cell "A2" value which holds a horse name Tiger Roll
    objIE.document.getElementById("text-search").Value = Sheets("Sheet1").Range("A2").Value

    'place focus on the Go button
    objIE.document.getElementById("Submit").Focus

    'click the 'go' button
    objIE.document.getElementById("Submit").Click

End Sub
excel vba search click box
1个回答
1
投票

改进你的代码

你的问题是,当你点击提交按钮时,它没有启用。你必须启用它。如果你观察一下,当你手动在输入框中插入一个长度为> 3个字符的文本时,提交按钮被启用了。

其中一个可能的解决方案是使用 objIE.Document.parentWindow.ExecScript "jQuery(""#text-search"").trigger(""change"")"插入值后,在点击提交按钮之前。

这里是完整的代码。

Sub HorseSearch()

    'Dim objIE As Object
    'Set objIE = CreateObject("InternetExplorer.Application")

    Dim obJe As InternetExplorer
    Set objIE = New InternetExplorer
    'make web page visible
    objIE.Visible = True

    'navigate objIE to this web page
    objIE.navigate "https://www.britishhorseracing.com/racing/horses/racehorse-search-results/"

    'wait here a few seconds while the browser is busy
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

    'in the search box put cell "A2" value which holds a horse name Tiger Roll

    objIE.Document.getElementById("text-search").Value = Sheets("Sheet1").Range("A2").Value
    On Error Resume Next
    objIE.Document.parentWindow.ExecScript "jQuery(""#text-search"").trigger(""change"")"
    On Error GoTo 0

    'click the 'go' button
    objIE.Document.getElementById("Submit").Click


End Sub

另一种解决方案

我们可以直接进入结果页,为什么要使用表单呢?结果页是这样的https://www.britishhorseracing.com/racing/horses/racehorse-search-results/#!?pagenum=1&q=tiger%20roll&rated=false

其中 q 参数值是你在输入框中输入的文本。我们可以直接导航到那个页面(使用单元格中的值)。A22 对于 q URL中的参数)。)

Sub HorseSearch()

    'Dim objIE As Object
    'Set objIE = CreateObject("InternetExplorer.Application")

    Dim obJe As InternetExplorer
    Set objIE = New InternetExplorer
    'make web page visible
    objIE.Visible = True

    'navigate objIE to this web page
    objIE.navigate "https://www.britishhorseracing.com/racing/horses/racehorse-search-results?pagenum=1&q=" & Sheets("Sheet1").Range("A2").Value & "&rated=false"

    'wait here a few seconds while the browser is busy
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

    'You have loaded the result page :)

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.