【问题标题】:How do I interact with this Internet Explorer Dropdown ComboBox Navigation using VBA Macro?如何使用 VBA 宏与此 Internet Explorer 下拉组合框导航交互?
【发布时间】:2019-09-05 13:59:55
【问题描述】:

我在尝试使用 Excel 宏在 Internet Explorer 中自动化下拉组合框时遇到了困难。请注意,我对 VBA 语言和网络抓取非常陌生。

我使用失败的代码是:

'Change Posting Period

Dim posting As Object
Set posting = ie.Document.getElementById("inpt_postingperiod2")
posting.Value = ActiveWorkbook.Sheets("RMA").Range("C5").Value

'The value in this cell C5 in this instance is: Aug 2019

在 Internet Explorer 中,此代码将更改下拉框中显示的文本,但当我稍后提交页面时,基础值从未真正更改过。

我尝试将fireevents添加到代码中,例如:

posting.fireevent ("onfocus")
posting.fireevent ("onkeydown")

没有成功

下拉相关的脚本是:

<input name="inpt_postingperiod" title="Sep 2019" class="dropdownInput textbox uir-field-active" id="inpt_postingperiod2" role="combobox" aria-labelledby="postingperiod_fs_lbl" style="width: 280px; background-color: rgb(255, 255, 255);" contenteditable="false" onkeydown="getDropdown(this).handleKeydown(event);" onkeypress="getDropdown(this).handleKeypress(event);" onfocus="getDropdown(this).handleOnFocus(event);" onblur="getDropdown(this).handleOnBlur(event);" type="text" value="Sep 2019" autocomplete="off">

以及下拉选项项目的脚本:

<div class="ns-dropdown" style="display: none;" data-name="postingperiod" data-settings='{"flags":-2147483520,"width":280,"minWidth":280,"mandatory":true}' data-default-option="4" data-options='[{"value":"127","text":"May 2019"},{"value":"128","text":"Jun 2019"},{"value":"129","text":"Jul 2019"},{"value":"131","text":"Aug 2019"},{"value":"132","text":"Sep 2019"},{"value":"133","text":"Oct 2019"},{"value":"138","text":"Nov 2019"},{"value":"139","text":"Dec 2019"},{"value":"140","text":"Jan 2020"},{"value":"142","text":"Feb 2020"},{"value":"143","text":"Mar 2020"},{"value":"144","text":"Apr 2020"}]' data-initialized="T">
</div>

请注意,应从下拉选项列表中选择 2019 年 8 月的 Excel 值。因此,为了自动执行此操作,我不能使用值/索引号,因为这将在未来发生变化;我需要将 MMM YYYY 的 excel 单元格值与下拉列表中的 MMM YYYY 匹配(在此示例中为 2019 年 8 月)。

感谢大家的帮助!

完整的代码应该是:

<tr>

		<td>

<div class="uir-field-wrapper" data-field-type="select"><span id="postingperiod_fs_lbl_uir_label" class="smallgraytextnolink uir-label "><span id="postingperiod_fs_lbl" class="labelSpanEdit smallgraytextnolink" style="">

<a tabindex="-1" title="What's this?" href="javascript:void(&quot;help&quot;)" style="cursor:help" onclick="return nlFieldHelp('Field Help','postingperiod', 'TRAN_CUSTCRED_postingperiod_NA', this, 'EDIT_TRAN_CUSTCRED', '3905005', 'custcred', 136, 'creditmemo', 'T', '', 'Credit+Memo', 'APP:FORMLABEL:POSTING_PERIOD', '')" class="smallgraytextnolink" onmouseover="this.className='smallgraytext'; return true;" onmouseout="this.className='smallgraytextnolink'; ">Posting Period</a>
<img class="required_icon" title="Required Field" alt="Required Field" src="/images/chiles/pageTitle/required.png"><label class="uir-required-icon">*</label></span></span><span class="uir-field">
        <span id="postingperiod_fs" data-fieldtype="select" class="nldropdown" style="white-space:nowrap;" data-helperbutton-count="0"><div class="uir-select-input-container"><input name="inpt_postingperiod" type="text" value="Sep 2019" class="dropdownInput textbox" onkeydown="getDropdown(this).handleKeydown(event);" onkeypress="getDropdown(this).handleKeypress(event);" onfocus="getDropdown(this).handleOnFocus(event);" onblur="getDropdown(this).handleOnBlur(event);" aria-labelledby="postingperiod_fs_lbl" autocomplete="off" contenteditable="false" role="combobox" id="inpt_postingperiod2" title="Sep 2019" style="width: 280px; background-color: rgb(255, 255, 255);"><span class="ddarrowSpan">
	<img src="/uirefresh/img/field/dropdown.png" height="20" width="20" class="i_dropdownarrow" alt="More Options" id="inpt_postingperiod2_arrow">
</span></div>

<div class="ns-dropdown" data-name="postingperiod" data-initialized="T" data-options="[{&quot;value&quot;:&quot;127&quot;,&quot;text&quot;:&quot;May 2019&quot;},{&quot;value&quot;:&quot;128&quot;,&quot;text&quot;:&quot;Jun 2019&quot;},{&quot;value&quot;:&quot;129&quot;,&quot;text&quot;:&quot;Jul 2019&quot;},{&quot;value&quot;:&quot;131&quot;,&quot;text&quot;:&quot;Aug 2019&quot;},{&quot;value&quot;:&quot;132&quot;,&quot;text&quot;:&quot;Sep 2019&quot;},{&quot;value&quot;:&quot;133&quot;,&quot;text&quot;:&quot;Oct 2019&quot;},{&quot;value&quot;:&quot;138&quot;,&quot;text&quot;:&quot;Nov 2019&quot;},{&quot;value&quot;:&quot;139&quot;,&quot;text&quot;:&quot;Dec 2019&quot;},{&quot;value&quot;:&quot;140&quot;,&quot;text&quot;:&quot;Jan 2020&quot;},{&quot;value&quot;:&quot;142&quot;,&quot;text&quot;:&quot;Feb 2020&quot;},{&quot;value&quot;:&quot;143&quot;,&quot;text&quot;:&quot;Mar 2020&quot;},{&quot;value&quot;:&quot;144&quot;,&quot;text&quot;:&quot;Apr 2020&quot;}]" data-default-option="4" data-settings="{&quot;flags&quot;:-2147483520,&quot;width&quot;:280,&quot;minWidth&quot;:280,&quot;mandatory&quot;:true}" style="display: none; ">
</div>





<input name="postingperiod" type="hidden" class="nldropdown" value="132" onchange="this.isvalid=(nlapiValidateField(null,'postingperiod'));if (!this.isvalid) return false;if(!this.noslaving) { setWindowChanged(window, true); }if (Syncpostingperiod(true,null,null,null,null,null) == false) return false;if (getEventTarget(event)==this)this.focus();var kPeriod = nlapiGetFieldValue('postingperiod');
if (nlapiGetFieldValue('ppsetbyuser') != 'T' || kPeriod != nlapiGetFieldValue('ppsetbyuservalue')) {
	if ( nlapiGetField('postingperiod').noSlaving() || nlapiGetFieldValue('ppsetbyuser') == 'S' ) {
var kPeriod = nlapiGetFieldValue('postingperiod');
if (kPeriod &amp;&amp; typeof pp_ss !== 'undefined' &amp;&amp; pp_ss[1132] !== undefined) {
	var iPeriod = + kPeriod + 1000;
	var bUseCurrent = (pp_ss[iPeriod] === undefined)
	if (!bUseCurrent) {
	var td = stringtodate(nlapiGetFieldValue('trandate'));
	var pp_s = stringtodate(pp_ss[iPeriod]);
	var pp_e = stringtodate(pp_es[iPeriod]);
	var cur_s = stringtodate(pp_es[1132]);
		bUseCurrent = !(pp_s <= td &amp;&amp; pp_e >= td) &amp;&amp; cur_s > pp_s;
	}
	if (bUseCurrent)
		nlapiSetFieldValue('postingperiod', 132, false);
}
nlapiSetFieldValue('ppsetbyuser', 'F', false);
nlapiSetFieldValue('ppsetbyuservalue', '', false);
	} else if (kPeriod) {
nlapiSetFieldValue('ppsetbyuser', 'T', false);
nlapiSetFieldValue('ppsetbyuservalue', nlapiGetFieldValue('postingperiod'), false);
	}
}
;nlapiFieldChanged(null,'postingperiod');" id="hddn_postingperiod2">

<input type="hidden" value="4" id="indx_postingperiod2">

<script type="text/javascript">
    (function () {
        var win = (window.parentAccesible != false && parent.initializeDropdowns) ? parent : window;
        win.initializeDropdowns(win.NS.jQuery(document));
    }())
</script><span class="field_widget_helper_pos effectStatic"></span></span>
</span>
</div>
		</td>

	   </tr>

注意,下拉选项不会显示,因为它们是从受密码保护的网站生成的,但选项只是 MMM YYYY 格式的日期

我也试过了:

Dim posting As Object
Set posting = ie.document.querySelector("#inpt_postingperiod2")
posting.Value = ActiveWorkbook.Sheets("RMA").Range("C5").Value
posting.fireevent ("onchange")

没有成功:我认为更改文本值不会支持下拉菜单并进行选择。将鼠标悬停在下拉菜单上时,您可以看到这一点。尽管它说的是 2019 年 8 月,但实际上是 2019 年 9 月:

enter image description here

提交前未正确选择下拉菜单的图片

【问题讨论】:

  • 可以分享一下网址吗?
  • 该网址需要用户名和密码,但我最近刚刚编辑了我的帖子以包含下拉菜单的完整代码。
  • 尝试将 html 事件创建为 change,然后在 ie.document.querySelector("#inpt_postingperiod2") 上设置值触发该事件后,请参阅 here
  • 如果您查看我的链接,它会创建事件并调度它。
  • 我尝试了这个但没有成功:[Dim 张贴为对象集张贴 = ie.document.querySelector("#inpt_postingperiod2") 张贴.Value = ActiveWorkbook.Sheets("RMA").Range(" C5").Value posting.fireevent("onchange")] 我认为因为它是一个下拉列表,粘贴文本和触发事件在下拉列表中找不到匹配项!

标签: excel vba internet-explorer


【解决方案1】:

它可能正在使用任何事件来设置下拉列表中的选定值。

我们可以看到,在您的下拉代码中有很多类似的事件

onkeydown="getDropdown(this).handleKeydown(event);" onkeypress="getDropdown(this).handleKeypress(event);" onfocus="getDropdown(this).handleOnFocus(event);" onblur="getDropdown(this).handleOnBlur(event);"

因此,您可以尝试从您的 VBA 代码中触发这些事件,以检查并验证哪个事件正在更改并在下拉列表中设置值。

您可以从 VBA 触发事件,如下所示。

Element.FireEvent("onkeydown") 

您上面的下拉代码没有生成下拉列表。如果您提供有效的下拉代码,我们可以尝试对其进行测试并尝试开发示例 VBA 代码来设置其中的值。

【讨论】:

  • 我尝试了 FireEvent 但没有成功,不确定要触发哪个元素!我还添加了下拉列表的完整代码,但选项被阻止(可能是因为它是受密码保护的站点)
  • 您需要发布适当的 JS 和 CSS 代码才能使该组合框正常工作。如果可能的话,请尝试在示例 HTML 页面中自己创建类似类型的组合框并将其发布在此处。我们用它做测试会更容易。您需要在组合框元素上触发事件,并且需要对该组合框上应用的每个事件进行测试,以找到设置其中值的正确事件。
猜你喜欢
  • 2012-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-23
  • 2019-10-20
相关资源
最近更新 更多