Unable to find a way to click on the drop-down menu to select any preferred item

I wrote code in vba using IE to select the England Premier League from the drop-down list that is currently displayed as Next To Play . My goal is to click on this drop-down link under the Next To Play heading, and then select the Australia A-League from option. However, I cannot find a way to click this link. If I run my script below as it is, it does not work and does not produce any errors and finally leaves the browser.

Link to this web page: URL

Here is what I have tried so far:

 Sub soccer() Dim IE As New InternetExplorer, html As HTMLDocument Dim post As Object With IE .Visible = True .navigate "replace_with_above_link" Do Until .readyState = READYSTATE_COMPLETE: Loop Set html = .document End With Application.Wait Now + TimeValue("00:00:05") For Each post In html.getElementsByTagName("option") '' If InStr(post.innerText, "Next To Play") > 0 Then MsgBox post.innerText: Exit For ''checked to see if I'm on the right place If InStr(post.innerText, "Next To Play") > 0 Then post.Click: Exit For Next post IE.Quit End Sub 

This is a partial image of the webpage where I want to trigger a click.

enter image description here

I would like to be able to click on this link and select any option.

I seem to be close to a solution. My below script can select the part that I want to execute:

 Sub Soccer_Dropdown() Dim IE As New InternetExplorer, html As HTMLDocument Dim post As Object With IE .Visible = True .navigate "https://ubet.com/sports/soccer/nexttoplay" Do Until .readyState = 4: DoEvents: Loop Set html = .document End With Application.Wait Now + TimeValue("00:00:05") Set post = html.querySelector(".ubet-fixed-price-navigator-selector-league select") post.Focus ' IE.Quit End Sub 

Result Image:

enter image description here

However, when it comes to performing a click, I can no longer do it.

+7
vba excel-vba click web-scraping
source share
3 answers

This is possible as a small extension to existing code.

I avoid using querySelector and querySelectorAll because they are scaly (for your other question reward).

I did not use Focus and used Click instead, since I assume that this runs regardless of what the script requires in the background.

Then you can manually select the correct index (using selectedIndex ) or search for children for the desired <label> value:

Work code:

 Option Explicit Sub Soccer_Dropdown() Dim IE As New InternetExplorer, html As HTMLDocument Dim divElement As Object, selectElement As Object, optionElement As Object Dim optionIndex As Long Dim targetOption As String ' load page With IE .Visible = True .navigate "https://ubet.com/sports/soccer/nexttoplay" Do Until .readyState = 4: DoEvents: Loop Set html = .document End With ' let page do its thing Application.Wait Now + TimeValue("00:00:05") ' get the div Set divElement = html.getElementsByClassName("ubet-fixed-price-navigator-selector-league") ' get the select in the div Set selectElement = divElement(0).getElementsByTagName("select")(0) ' click it to activate any script to populate select selectElement.Click ' select by index - this works but showing the other technique below 'selectElement.selectedIndex = 9 ' select by iterating the options optionIndex = 0 targetOption = "Australia A-League" For Each optionElement In selectElement If LCase(optionElement.localName) = "option" And optionElement.Label = targetOption Then selectElement.selectedIndex = optionIndex Debug.Print targetOption & " at index " & optionIndex Exit For End If optionIndex = optionIndex + 1 Next ' quit IE 'IE.Quit End Sub 

Edit

@QHarr - to your question How can Set selectElement = divElement (0) .getElementsByTagName ("select") (0) access the Australian League?

In the div containing the dropdown there is a class ubet-fixed-price-navigator-selector-league and that <div> has a single <select> element which is a dropdown list. Since getElementsByTagName returns a set of HTML elements, we just want to return the first <select> element to a <div> . Index 0 does not apply to the list of things in the drop-down list (i.e. <option> s). See HTML:

enter image description here

A bit further down the HTML, you can see the <option> elements (grouped into <optgroup> s), where you can access the dropdown values:

enter image description here

@Topto - a good question - the localName property, as well as several others for the optionElement variable, was what I could see in the Locals window as having an option value:

enter image description here

I think you could use nodeName same as localName . There is also tagName , which I suppose is the most obvious sound:

enter image description here

NTN

+2
source share

Does a web browser absolutely need a web browser GUI? Let me doubt ...

If you try to debug the landing page , you will see that the data you are looking for are JSON types and are the result of an XHR request.

Suppose you have all the necessary cookies to send an XHR request to the target site, you can directly receive JSON data (from curl) instead of viewing the website ...


Apparently, each league has a unique identifier (50 for the English, 76 for Australia, etc.)

So, in order to access Australia A-League data, you need to send a GET request:

https://ubet.com/api/sportsViewData/nexttoplay/false/3/76 Australia A-League


In this resulting JSON file, you will get the Offers key, which contains a dict that contains all the sentences and also contains all the required data, which I expect you to brighten up ....

See an example sentence:

 LongDisplayName "Central Coast Mariners" DisplayOrder 1 OfferOrder 1 HAD 1 PlaceDeduction 0 ShowOfferBasedDynamicOffer false OfferId 308151 OfferName "Central Coast" DisplayHAD 1 WinReturn 1.85 PlaceReturn 0 DynamicOffer null Status "o" 

Hi

+1
source share

Going to https://ubet.com/sports/soccer/england-premier-league allows you to get this web page.

I know this is a short answer. I looked at this for a while (although the answer is short) and this is an Angular.js application, but I still can’t understand how it works. Somehow you need to call vm.selectedKeyChange manually. I'm working on it.

+1
source share

All Articles