I'm looking to automate Internet Explorer using Excel VBA to retrieve soccer results from a website, and I really struggle to keep the data updated when the drop-down list value changes.
Website: http://www.whoscored.com/Regions/250/Tournaments/30/Seasons/3871/Stages/8209/Fixtures/Europe-UEFA-Europa-League-2013-2014
I want to change the value of the "stage" drop-down list and clear the results of the match.
My code works fine for opening IE by changing the value of the "scrape" drop-down list, but I cannot get the data to update. Although I like VBA, I know very little about HTML and Javascript, although I can guess what some lines do. From what I see, there is javascript code that processes the change event, I just canβt figure out how to fire it - I tried to fire the βonchangeβ event in my code, as suggested in my searches, but I canβt force its work.
This is the code that I see that controls the dropdown list (I removed a lot of dropdown values ββfor the other dropdowns as this made this message even longer:
<div id="breadcrumb-nav"> . . <span><select id="stages" name="stages"><option selected="selected" value="/Regions/250/Tournaments/30/Seasons/3871/Stages/8209">Europa League Group Stages</option> <option value="/Regions/250/Tournaments/30/Seasons/3871/Stages/7816">Europa League Qualification</option> <option value="/Regions/250/Tournaments/30/Seasons/3871/Stages/8158">Europa League Grp. A</option> <option value="/Regions/250/Tournaments/30/Seasons/3871/Stages/8159">Europa League Grp. B</option> . . <option value="/Regions/250/Tournaments/30/Seasons/3871/Stages/8466">Europa League</option> </select></span> </div> <script type="text/javascript"> $('#breadcrumb-nav select').change(function () { NG.GA.trackEvent('BreadcrumbNav', this.id); window.location.href = this.value; </script>
my code is:
Sub ScrapeData() Dim ie As InternetExplorer Dim URL As String URL = "http://www.whoscored.com/Regions/250/Tournaments/30/Seasons/3871/Stages/8466/Fixtures/Europe-UEFA-Europa-League-2013-2014" Set ie = New InternetExplorer ie.Visible = True ie.navigate (URL) Do DoEvents Loop Until ie.readyState = 4 SelectValue ie, "/Regions/250/Tournaments/30/Seasons/3871/Stages/7816" SelectValue ie, "/Regions/250/Tournaments/30/Seasons/3871/Stages/8209" End Sub Sub SelectValue(ByVal ie As InternetExplorer, ByVal value As String) Dim htmlDoc As HTMLDocument Dim ddStages As HTMLSelectElement Dim idBreadCrumb As Object Set htmlDoc = ie.document With ie.document Set idBreadCrumb = .getelementbyid("breadcrumb-nav") Set ddStages = .getelementbyid("stages") End With ddStages.value = value ddStages.FireEvent ("onchange") 'fireevent on ddStages didn't work so tried here too idBreadCrumb.FireEvent ("onchange") Do DoEvents Loop Until ie.readyState = 4 End Sub
Any help would be really appreciated.
vba automation scrape
Ash
source share