首页 > 解决方案 > 如何在 Excel 中使用 VBA 放置焦点并单击提交按钮?

问题描述

我是网络抓取和 VBA 的新手,但我渴望进一步学习。我有一个小项目,正在一步一步地慢慢完成。我在一个几乎可以工作的 Excel 模块中有一小段代码。我只是无法让它提交我的搜索框文本。我的 Excel 工作表中的单元格 A2 中基本上有示例马名。该代码会打开网站并将文本输入到搜索框中。我只是无法让它单击“开始”按钮。我将不胜感激任何帮助和解释我做错了什么,这样我就可以再次避免它!

当检查网站上的 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

标签: excelvbasearchclickbox

解决方案


改进你的代码

您的问题是,当您点击提交按钮时,它未启用。您必须启用它。如果您观察到它,当您手动将文本插入输入框中时,提交按钮已启用,长度 > 3 个字符。您的代码插入更改输入框的值属性的文本,但它不会触发“onchange”事件。在 Chrome 中使用 Web 开发人员工具,我看到表单行为(输入框与提交按钮)是在 jQuery 中构建的。

一种可能的解决方案是 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参数值是您在输入框中输入的文本。我们可以直接导航到该页面(使用单元格中的值作为 URL 中A22q参数)。

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

推荐阅读