Excel VBA - 在搜索框中输入的文本不会导致页面显示结果(搜索没有提交按钮)

时间:2022-09-10 20:38:23

I am quite new to coding and have the following issue: I want to write a macro, that goes to the website: https://displaypurposes.com/ inserts a keyword and copy the results into my excel sheet.

我对编码很陌生并且遇到以下问题:我想写一个宏,进入网站:https://displaypurposes.com/插入关键字并将结果复制到我的Excel工作表中。

This is how my code looks at the moment:

这就是我的代码目前的样子:

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

I figured out that I might be able to solve the problem with .fireEvent(), Sendkeys or the solution posted by dyanisis2

我发现我可以用.fireEvent(),Sendkeys或dyanisis2发布的解决方案解决问题

Set evt = objIE.Document.createEvent("keyboardevent")
    evt.initEvent "change", True, False
    PW.all(0).dispatchEvent evt

However, I didn't manage to make the page show the searchresults for the keyword so I could scrape them. Since I am quite new to the community I hope I posted everything according to the guidelines and answered everything you need to know!

但是,我没有设法让页面显示关键字的搜索结果,所以我可以抓它们。由于我对社区很陌生,我希望我按照指南发布所有内容并回答你需要知道的一切!

2 个解决方案

#1


1  

Looking at the way the search for this web page works, is that it requires one letter at a time to complete the search.

查看搜索此网页的方式是,它一次需要一个字母才能完成搜索。

You can set the focus to the 'input' tag name and then use send keys to complete the search:

您可以将焦点设置为“输入”标签名称,然后使用发送键完成搜索:

objIE.document.getElementsByTagName("input")(0).Focus
Application.SendKeys "s"
Application.SendKeys "e"
Application.SendKeys "a"
Application.SendKeys "r"
Application.SendKeys "c"
Application.SendKeys "h"

Now you know how it works, you can use this code so that it will work with the keyword value:

现在您知道它是如何工作的,您可以使用此代码,以便它可以使用关键字值:

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

#2


0  

To trigger the fireEvent on the field, you have to type your string (you've already figured that out). Before you type, set the focus to the field:

要在字段上触发fireEvent,您必须键入字符串(您已经想到了这一点)。在键入之前,将焦点设置为字段:

objIE.document.getElementsByTagName("input")(0).Focus

I would add a click in there as well just to be safe

我会在那里添加一个点击以确保安全

objIE.document.getElementsByTagName("input")(0).Click

Now all you have to do is send the keys:

现在你所要做的就是发送密钥:

Dim sMyString As String: sMyString = "#mystring"
Dim iC As Integer
For iC = 1 To Len(sMyString)
    Application.SendKeys Mid(sMyString, iC, 1)
Next

#1


1  

Looking at the way the search for this web page works, is that it requires one letter at a time to complete the search.

查看搜索此网页的方式是,它一次需要一个字母才能完成搜索。

You can set the focus to the 'input' tag name and then use send keys to complete the search:

您可以将焦点设置为“输入”标签名称,然后使用发送键完成搜索:

objIE.document.getElementsByTagName("input")(0).Focus
Application.SendKeys "s"
Application.SendKeys "e"
Application.SendKeys "a"
Application.SendKeys "r"
Application.SendKeys "c"
Application.SendKeys "h"

Now you know how it works, you can use this code so that it will work with the keyword value:

现在您知道它是如何工作的,您可以使用此代码,以便它可以使用关键字值:

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

#2


0  

To trigger the fireEvent on the field, you have to type your string (you've already figured that out). Before you type, set the focus to the field:

要在字段上触发fireEvent,您必须键入字符串(您已经想到了这一点)。在键入之前,将焦点设置为字段:

objIE.document.getElementsByTagName("input")(0).Focus

I would add a click in there as well just to be safe

我会在那里添加一个点击以确保安全

objIE.document.getElementsByTagName("input")(0).Click

Now all you have to do is send the keys:

现在你所要做的就是发送密钥:

Dim sMyString As String: sMyString = "#mystring"
Dim iC As Integer
For iC = 1 To Len(sMyString)
    Application.SendKeys Mid(sMyString, iC, 1)
Next