HTMLInputElement 被点击,HTMLOptionElement 被点击

发布于 2024-08-24 06:21:04 字数 1027 浏览 5 评论 0原文

我正在尝试编写 vba 代码来填写网络表单并为我单击按钮。我循环浏览页面上的各种 option 标签以选择我想要的标签。当我到达它时,我想选择它,但我不确定语法。

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

以下是网页中的 HTML:

<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>

我想选择选项“INS”。

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".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

把回忆走一遍 2024-08-31 06:21:04
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
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
雄赳赳气昂昂 2024-08-31 06:21:04

我没有使用过 VBA,所以我提前道歉,但假设它使用与我熟悉的其他语言相同的 DOM 结构,请尝试:

htmlO.selected= "selected"

这就是我在 javascript 中实现此目的的方法:)

HTH

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

弄潮 2024-08-31 06:21:04

CSS 选择器:

这是一种更简单的方法。使用 option[value='INS'] CSS 选择器。
这表示选择带有 option 标记的元素,该元素的属性 value 的值为 'INS'


CSS 查询:

query


VBA:

CSS 选择器 通过 document 的“nofollow noreferrer">.querySelector 方法。

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

或者

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

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:

query


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 
浅听莫相离 2024-08-31 06:21:04

最终起作用的是......

将 htmlO 调暗为 HTMLSelectElement
设置 htmlS = objdoc.getElementById("菜单")
htmlS.selectedIndex = 7

我必须参考整个菜单并选择要选择的菜单,而不是选择单个选项。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文