我对编码很陌生并且遇到以下问题: 我想写一个宏,进入网站:https://displaypurposes.com/ 插入关键字并将结果复制到我的Excel工作表中。
这就是我的代码现在的样子:
Sub DisplayPurposes()
Dim objIE As InternetExplorer
Dim aEle As HTMLLinkElement
Dim Url As String
Dim Keyword As String
Url = "https://displaypurposes.com/"
Keyword = "skiing"
'initiating a new instance of Internet Explorer and assigning it to objIE
Set objIE = New InternetExplorer
'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True
'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate (Url)
'Wait for IE to load page
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
'BOTH OF THESE INSERT THE KEYWORD
'Insert Version 01
objIE.document.getElementsByTagName("input")(0).innerText = "Keyword"
'Insert Version
objIE.document.getElementsByTagName("input")(0).Value = "Keyword"
'NOW I DON'T KNOW HOW TO MAKE THE PAGE UPDATE ITSELF
End Sub
我发现我可以用.fireEvent(),Sendkeys或dyanisis2发布的解决方案来解决问题
Set evt = objIE.Document.createEvent("keyboardevent")
evt.initEvent "change", True, False
PW.all(0).dispatchEvent evt
但是,我没有设法让页面显示关键字的搜索结果,所以我可以抓它们。由于我对社区很陌生,我希望我按照指南发布所有内容并回答你需要知道的一切!
答案 0 :(得分:1)
查看搜索此网页的方式是,它一次需要一个字母才能完成搜索。
您可以将焦点设置为“输入”标签名称,然后使用发送键完成搜索:
objIE.document.getElementsByTagName("input")(0).Focus
Application.SendKeys "s"
Application.SendKeys "e"
Application.SendKeys "a"
Application.SendKeys "r"
Application.SendKeys "c"
Application.SendKeys "h"
现在您知道它是如何工作的,您可以使用此代码,以便它可以使用关键字值:
objIE.document.getElementsByTagName("input")(0).Focus
Dim i As Integer
Dim EachLetter As Variant
EachLetter = Split(Keyword, " ")
For i = 0 To UBound(EachLetter)
Application.SendKeys EachLetter(i)
Next i
答案 1 :(得分:0)
要在字段上触发fireEvent
,您必须输入字符串(您已经知道了)。在键入之前,将焦点设置为字段:
objIE.document.getElementsByTagName("input")(0).Focus
我会在那里添加一个点击以保证安全
objIE.document.getElementsByTagName("input")(0).Click
现在你所要做的就是发送钥匙:
Dim sMyString As String: sMyString = "#mystring"
Dim iC As Integer
For iC = 1 To Len(sMyString)
Application.SendKeys Mid(sMyString, iC, 1)
Next