0

I know there have been similar questions on SO about this issue, and I have been reviewing and trying many of them, but still, I'm having an issue selecting an option from a drop-down list on a website. I'm really trying to figure this out on my own, but I can't seem to get this part right.

Unfortunately, the website can only be accessed internally (of course!), so I can't share it, but I've tried to provide the HTML that I think is relevant.

What I'm trying to do is select the "Equals To" option from the list. Most of my attempts end up skipping the IF = Equals To part and advances to the next part of the VBA.

Here are a couple of my more recent attempts. (Getting an Object Required error on the QuerySelector approaches.

<!DOCTYPE html>
<html>

<body class="bootstrap-wrapper" style="cursor: auto;">
  <div tabindex="-1" class="ui-dialog ui-corner-all ui-widget ui-widget-content ui-front dialog ui-draggable ui-resizable" role="dialog" aria-describedby="02T" aria-labelledby="ui-id-1" style="left: 328px; top: 167.5px; width: 977px; height: 603px; overflow: visible; display: block; position: absolute; z-index: 201;">
    <div title="" class="chosen-container chosen-container-single chosen-container-single-nosearch chosen-with-drop chosen-container-active" id="paymentReferenceNumber_chosen" style="left: 482px; top: 279.41px; width: 200px; margin-top: 45px; margin-left: 0px; position: absolute;">
      <div class="chosen-drop" style="display: block;">
        <div class="chosen-search"><input type="text" readonly="" autocomplete="off"></div>
        <ul class="chosen-results">
          <li class="active-result result-selected" data-option-array-index="0">
            Select
          </li>
          <li class="active-result" data-option-array-index="1">
            Equals To
          </li>
          <li class="active-result" data-option-array-index="2">
            Contains
          </li>
          <li class="active-result" data-option-array-index="3">
            Is Empty
          </li>
        </ul>
      </div>
    </div>
  </div>
</body>

</html>
Dim inputElement As HTMLInputElement
Set inputElement = doc.querySelector( _
    "html body[class=bootstrap_wrapper] tbody div[tabindex=-1] div[class=chosen-container chosen-container-single chosen-container-single-nosearch chosen-with-drop chosen-container-active] div[class=chosen-drop] ul[class=chosen-results] li[class=active-result][data-option-array-index=1]")

If Not inputElement Is Nothing Then
    inputElement.Click
End If

OR

ieApp.Document.querySelector("Select[name=paymentReferenceNumber_oper] option[value=equalsTo]").Selected = True

OR

    Set ElementCol = ieApp.Document.getElementsByTagName("li")
    For Each ee In ElementCol
        If ee.innerHTML = "Equals To" Then
           ee.Click: DoEvents: Sleep 1000
           Exit For
        End If
    Next ee

Most of the other links I have been able to select simply by using:

ieApp.Document.all.Item("adv_search_tab").Click: Sleep 1000

Or Using:

Set ElementCol = ieApp.Document.getElementsByTagName("input")
    For Each ee In ElementCol
        If ee.Name = "search" Then
           ee.Click: DoEvents: Sleep 1000
           Exit For
        End If
    Next ee

These are just two of the recent SO posts I have been reviewing to try to understand alternatives: VBA selecting a value from dropdown box on a webpage

Excel VBA change option in the HTML select tag

TBoulz
  • 339
  • 5
  • 20
  • Bit difficult to figure this out without the website but from what I can see, you are **clicking** on the box and not actually selecting a value. From memory, I think you might have to change your `For` to: `For Each ee In ElementCol.ChildNodes`.. haven't tested that so could be wrong – Zac Mar 22 '19 at 16:21
  • @Zac thank you for the advice, but when I make that change I receive the error `Object doesn't support this property or method`. I'm looking up more info on using `ChildNodes` but I keep receiving this error. – TBoulz Mar 22 '19 at 17:21
  • your querySelector is correct if a little long. What is happening when you run that? – QHarr Mar 22 '19 at 19:49
  • @QHarr sorry for the delay, just got back to work. Using the querySelector approach I get the `Object Required` error. A few of the other approaches I tried above will not result in an error, but fail to find the element. – TBoulz Mar 26 '19 at 12:42
  • @QHarr I updated my post to include a better idea of the HTML snippet, and anothery `Query Selector` I'm trying – TBoulz Mar 27 '19 at 15:34
  • Have you tried adding a wait before using your selector? Also, check the item is not inside an iframe/frame – QHarr Mar 27 '19 at 15:35
  • I do have a loop if IE is busy, but I also get the error if stepping through the code. When I'm in the DOM Explorer I didn't see mention of an iframe, but after expanding some of the fields I do think that this is within an iframe. Does a `Query Selector` not work within an iframe? – TBoulz Mar 27 '19 at 16:40

0 Answers0