开发者

An HTMLInputElement gets clicked, and an HTMLOptionElement gets

开发者 https://www.devze.com 2022-12-22 06:34 出处:网络
I\'m am trying to write vba code to fill out web-forms and click buttons for开发者_运维知识库 me. I am looping through the various option tags on the page to select the one I want. When I reach it, I

I'm am trying to write vba code to fill out web-forms and click buttons for开发者_运维知识库 me. I am looping through the various option tags on the page to select the one I want. When I reach it, I want to select it, but I'm not sure of the syntax.

Dim htmlO As HTMLOptionElement
For Each htmlO In Object.getElementByTagName("option")
    If Trim(htmlO.value) = "INS" Then
       htmlO.????  (click? select?)
        Exit For
    End If
Next

Here is the HTML from the webpage:

<select gtbfieldid="40" id="menu" name="pv_choice">
<option selected="selected" value="ZZZ">Choose Menu Option
</option><option value="BL_COMP">Blanket Companies
</option><option value="CARR_SEARCH">Carrier Search
</option><option value="PASSWORD">Change Password
</option><option value="FED_REG">FMCSA Register
</option><option value="FEEDBACK">Feedback
</option><option value="HOME">Home Page
</option><option value="INS">Insurance Filing
</option><option value="OOS_LIST">Out Of Service Carriers
</option></select>

I want to select the option "INS".


Dim StrFindText  as string
Dim htmlSelect As HTMLSelectElement
Dim htmlItemOption As IHTMLOptionElement

Set htmlSelect  = Object.getElementById("menu")
StrFindText = "INS"

For i = 0 To htmlSelect.options.length - 1
    htmlItemOption = htmlSelect.options(i)
    ' UCase(htmlItemOption.text) = UCase(StrFindText) ' if find text options
    If UCase(htmlItemOption.value) = UCase(StrFindText) Then
       htmlItemOption.selected = True
       Exit For
    End If
Next i


I haven't used VBA so I apologize in advance but assuming it uses the same structure for the DOM than other languages I'm familiar with, try:

htmlO.selected= "selected"

which is how I would achieve this in javascript :)

HTH


CSS selector:

Here is a much simpler way. Use a CSS selector of option[value='INS']. This says select element with option tag that has attribute value whose value is 'INS'.


CSS query:

An HTMLInputElement gets clicked, and an HTMLOptionElement gets


VBA:

The CSS selector is applied via the .querySelector method of document.

objdoc.querySelector("option[value='INS']").Click  

or

objdoc.querySelector("option[value='INS']").Selected = True 


What worked in the end was...

Dim htmlO As HTMLSelectElement
Set htmlS = objdoc.getElementById("menu")
htmlS.selectedIndex = 7

I had to reference the entire menu and choose which one to select, not select the individual option.

0

精彩评论

暂无评论...
验证码 换一张
取 消