使用VBA在javascript下拉菜单中选择一个选项

时间:2018-04-06 02:18:56

标签: javascript html vba

使用宏从此下拉菜单中选择“已关闭”:

<div class="modal ui-dialog-content ui-widget-content" id="pausingModal" scrolltop="0" scrollleft="0" style="width: auto; min-height: 0px; height: auto;">
<label>Please specify the reason</label>
<div class="row">
<div class="select2-container select2 reason select2-container-active select2-dropdown-open" id="s2id_autogen2" style="width: 100%"><a href="javascript:void(0)" class="select2-choice select2-default" tabindex="-1">   <span class="select2-chosen" id="select2-chosen-3">Select a reason</span><abbr class="select2-search-choice-close"></abbr>   <span class="select2-arrow" role="presentation"><b role="presentation"></b></span></a><label for="s2id_autogen3" class="select2-offscreen"></label><input class="select2-focusser select2-offscreen" type="text" aria-haspopup="true" role="button" aria-labelledby="select2-chosen-3" id="s2id_autogen3" disabled=""></div><select class="select2 reason select2-offscreen" style="width: 100%" tabindex="-1" title="">
<option></option>
<optgroup label="Related">
<option data-pause-type="hard_pause" data-show-text-area="false" value="1">
Closed
</option>

我尝试了不同的解决方案,尝试检索所有选项名称,然后选择innertext但该选项未在菜单中选中:

For Each obj In objIE.Document.getElementsByClassName("select2 reason").getElementsByTagName("option")
If obj.innerText = "Closed" Then
obj.Selected = True
End If
Next

For Each obj In objIE.Document.getElementsByClassName("select2 reason").getElementsByTagName("option")
If obj.Value = "1" Then
obj.Selected = True
End If
Next

我尝试了onchange事件:

Dim o
For Each o In objIE.Options
    If o.Value = "1" Then
        o.Selected = True
        Exit For
    End If
Next

Dim objEvent
Set objEvent = objIE.Document.createEvent("HTMLEvents")
objEvent.initEvent "change", False, True
objIE.dispatchEvent objEvent

编辑:事实证明代码中有javascript,因此我修改如下:

For Each obj In objIE.Document.getElementsByTagName("span")
If obj.id = "select2-chosen-3" Then
obj.Focus
obj.innerText = "Business Closed Permanently"
Exit For
End If
Next

它会填充菜单,但是当我点击它时就像没有被选中一样。

然后我尝试添加:

For Each obj In objIE.Document.getElementsByTagName("a")
If obj.className = "select2-choice select2-default" Then
obj.Click
Exit For
End If
Next

但这不起作用。

0 个答案:

没有答案