如何在Excel中使用VBA放置焦点并单击“提交"按钮? [英] How do I place focus and click submit button using VBA in Excel?
问题描述
我是Web抓取和VBA的新手,但渴望进一步学习.我的脑子里有个小项目,并且每次都在慢慢地进行.我在几乎可以正常工作的Excel模块中有一小段代码.我只是无法让它提交我的搜索框文字.我的Excel工作表中的单元格A2中基本上有示例马名.该代码将打开网站,并在搜索框中输入文字.我只是无法单击转到"按钮.非常感谢您的帮助和对我做错事情的解释,这样我就可以避免再次发生!
I am new to web scraping and VBA but am eager to learn further. I have a small project in mind and am taking it slowly one step at a time. I have a small piece of code in an Excel module which is almost working. I just cannot get it to submit my search box text. I basically have the example horse name in Cell A2 in my Excel worksheet. The code opens the website and also enters the text into the search box. I just can't get it to click the Go button. I'd appreciate any help and an explanation of what I'm doing wrong so I can avoid it again!
在检查网站上的转到"按钮时,HTML如下:
When inspecting the Go button on the site, the HTML is thus:
我的代码:
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个字符的文本时,提交按钮就会启用.您的代码插入了更改输入框的value属性的文本,但不会触发'onchange'事件.使用Chrome中的Web开发人员工具,我看到表单行为(输入框和提交按钮)是在jQuery中构建的.
Your problem is that when you fire the click on the submit button it is not enabled. You have to enable it. If you observe it the submit button got enabled when you manually insert a text into the input box, with a length > 3 characters. Your code insert the text changing the value attribute of the input box but it doesn't fire the 'onchange' event. Using web developer tools in Chrome I saw that the form behaviour (input box vs submit button) is built in jQuery.
一种可能的解决方案是使用 objIE.Document.parentWindow.ExecScript"jQuery("#text-search").trigger(""change")"
插入值后,然后单击提交"按钮.
One of the possible solutions can be to use
objIE.Document.parentWindow.ExecScript "jQuery(""#text-search"").trigger(""change"")"
after inserting the value and before clicking on the submit button.
这里是完整的代码:
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
Why to use the form when we can directly go to the result page?
Result page is something like
https://www.britishhorseracing.com/racing/horses/racehorse-search-results/#!?pagenum=1&q=tiger%20roll&rated=false
其中 q
参数值是您在输入框中输入的文本.我们可以直接导航到该页面(使用URL中的 q
参数的单元格 A22
中的值).
where the q
parameter value is the text you put in the input box. We could navigate directly to that page (using the value from cell A22
for q
parameter in the 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
这篇关于如何在Excel中使用VBA放置焦点并单击“提交"按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!