【问题标题】:How do I place focus and click submit button using VBA in Excel?如何在 Excel 中使用 VBA 放置焦点并单击提交按钮?
【发布时间】:2020-08-15 22:30:02
【问题描述】:

我是网络抓取和 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

【问题讨论】:

    标签: excel vba search click box


    【解决方案1】:

    改进您的代码

    您的问题是,当您点击提交按钮时,它未启用。您必须启用它。如果您观察到它,当您手动将文本插入输入框中时,提交按钮已启用,长度 > 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&amp;q=tiger%20roll&amp;rated=false

    其中q 参数值是您在输入框中输入的文本。我们可以直接导航到该页面(使用单元格 A22 中的值作为 URL 中的 q 参数)。

    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
    

    【讨论】:

    • 伊戈尔.......你是一个绝对的传奇!非常感谢您花时间与我分享您的知识。这无疑促使我现在继续前进!
    • Wowzers!......这很难让我明白!我的下一个问题虽然可能很简单,但它是一行代码,可以单击并激活带有马名的页面。要么我只是单击作为链接的马名,要么提取马的参考号并直接导航到马的统计页面。我好像也做不到.......
    • 我不太了解社区规则,但我认为最好发布一个新问题(在 cmets 中留下链接)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    相关资源
    最近更新 更多