VBA will change dropdown value in Internet Explorer

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; // TODO: Disable all selects? }); </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.

0
vba automation scrape
source share
1 answer

Some JavaScript script must be executed in the event, "the selection element changed its value." My suggestion, much simpler than running JavaScript, is to simply navigate the link (because the JS here just changes the HTML page you see, not the elements on the same web page).

So, for example, I would simply replace this:

 SelectValue ie, "/Regions/250/Tournaments/30/Seasons/3871/Stages/7816" 

with this

 ie.Navigate "http://www.whoscored.com/" & "/Regions/250/Tournaments/30/Seasons/3871/Stages/7816" 

to get exactly the same result.

0
source share

All Articles