excel - 如何在 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
解决方案
改进你的代码
您的问题是,当您点击提交按钮时,它未启用。您必须启用它。如果您观察到它,当您手动将文本插入输入框中时,提交按钮已启用,长度 > 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 中A22
的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
推荐阅读
- swift - 是否可以在 macOS 状态栏中放置矢量(或字体)图标?
- c# - .Net core中查询PK表时如何获取FK表数据?
- html - 如何以正确的方式对齐我的 flexbox 项目?
- cmyk - 如何使用 Magick.Net 将 RGB 位图数据保存为 CMYK tiff
- php - 将最小第二个偶数和最大第二个偶数相加:
- mysql - MYSQL 使用案例从 curdate() 中减去天数
- excel - VBA添加一个新工作表,如果名称存在添加一个数字
- javascript - 在 FormArray 中获取唯一值 - Angular
- r - 如何为重复值创建序列列?
- video - ffmpeg 使用图像和视频制作幻灯片