Excel VBA浏览器自动化:网站search框没有元素ID

我正在尝试使用Excel VBA进行浏览器自动化,但该网站( http://www.soccerstats.com/ )search框没有元素标识,所以通常的技术将无法使用。 这是我的计划代码:

'dimension (declare or set aside memory for) our variables Dim objIE As InternetExplorer 'special object variable representing the IE browser Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element Dim y As Integer 'integer variable we'll use as a counter Dim result As String 'string variable that will hold our result link 'initiating a new instance of Internet Explorer and asigning 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 "http://www.soccerstats.com/" '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, the word "in" and cell "C1" value objIE.document.getElementById("dsearch").Value = "arsenal" 

search框的html代码是:

<input name="searchstring" style="border: currentColor; border-image: none; width: 90px; height: 20px; padding-left: 5px; background-color: rgb(238, 238, 238); text-color: gray;" onfocus="if (this.value == 'Search team...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Search team...';}" type="text" maxlength="20" value="Search team...">

谁能告诉我如何把这个search框的值,然后单击searchbutton?

你可以遍历input元素,并find一个名为searchstring ,如下所示:

 Dim ele As Object For each ele in objIE.document.getElementsByTagName("input") If ele.Name = "searchstring" Then ele.Value = "Example Text" End if Next ele