Reputation: 35
I am trying to use Excel VBA for browser automation, but the website's (http://www.soccerstats.com/) search box has no element id, so the usual technique will not work. Here was my planned code:
'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"
The search box html code is:
<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...">
Can anyone tell me how to put a value for this search box and then click the search button?
Upvotes: 0
Views: 1154
Reputation: 4514
You could loop through the input elements and find the one named searchstring
, like this:
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
Upvotes: 1