我正在尝试通过Excel VBA代码进行IE自动化。 我有一个网页,其中包含可以增加价值的单元格。我需要通过代码vba excel更改这些单元格中的值 例: 现在,网页中的单元格具有值:“ 11_now test” 我的代码vba excel:
ie.Document.getelementbyid("mx7125[R:0"]").Value = "Good" ' value i want to set
运行vba时,网页在单元格上显示的值为“良好”,但是当我单击“保存”按钮时,单元格上的值为“ 11_now_test”
HTML代码如下:
<input aria-labelledby="" id="mx7125[R:0]" class="fld text tt" ctype="textbox" li="mx7126[R:0]" db="mx5134" maxlength="200" style=";width:300px;" ontr='true' async='1' ae="setvalue" type="text" title="Remark: 11_now_test" value="11_now_test" ov="11_now_test" work="1" fldInfo='{"length":"200","inttype":"0"}'/></div><div aria-live="polite" id="mx7126[R:0]_holder" class="bc"><img sf="1" aria-hidden="true" active="0" ctype="image" alt="" src="../webclient/skins/skins-20171010-1330/tivoli09/images/blank.gif" source="blank" imgtype=".gif" style="display:inline;margin:0px;" border="0" lc="mx7125[R:0]" align="absmiddle" width='0px' height='26px' id="mx7126[R:0]" title=""/></div></td>
*更多信息*
亲爱的兄弟 当我按F12到网页上的检查器时。如果单击以更改网页上的值,那么此时的html代码为:
<input aria-labelledby="" id="mx7125[R:0]" class="fld text tt" ctype="textbox" li="mx7126[R:0]" db="mx5134" maxlength="200" style=";width:300px;" ontr="true" async="1" ae="setvalue" type="text" title="Remark: 11 Nov test" value="11 Nov test" ov="11 Nov test" work="1" fldinfo="{"length":"200","inttype":"0"}" originalvalue="11 Nov test" prekeyvalue="good" stoptcclick="true" keydown="false" changed_by_user="true" changed="true">
当在网页上输入单元格时键入值“ Good”,则prekeyvalue =“ Good”并按下keydown键将其变为“ True”,然后按下keydown键将其返回到“ false”
*输入单元格处的Javascrip *
当单击以更改输入单元格的值时,我发现了javascrip:
function tb_(eventOrComponent)
{
eventOrComponent = (eventOrComponent) ? eventOrComponent : ((window.event) ? window.event : "");
var eventType = eventOrComponent.type;
var textbox = this;
if(undef(eventType) || eventType=="text") {
eventType = "init";
textbox = eventOrComponent;
}
if(DESIGNMODE)
return;
var ro = textbox.readOnly;
var exc=(textbox.getAttribute("exc")=="1");
switch(eventType)
{
case "init":
setPromptValue(textbox.id);
break;
case "mousedown":
if(getFocusId()==this.id)
this.setAttribute("stoptcclick","true");
break;
case "mouseup":
if (isIE() && !hasFocus(this))
{
this.focus();
}
if (isBidiEnabled)
{
adjustCaret(eventOrComponent, this);
}
break;
case "blur":
input_onblur(eventOrComponent,this);
if (isBidiEnabled)
input_bidi_onblur(eventOrComponent, this);
break;
case "change":
if(!ro)
input_changed(eventOrComponent,this);
break;
case "click":
if(overError(eventOrComponent,this))
showFieldError(eventOrComponent,this,true);
var liclick=this.getAttribute("liclick");
var li=this.getAttribute("li");
if(li!="" && liclick=="1")
{
frontEndEvent(getElement(li),'click');
}
if(this.getAttribute("stoptcclick")=="true")
{
eventOrComponent.cancelBubble=true;
}
this.setAttribute("stoptcclick","false");
break;
case "focus":
input_onfocus(eventOrComponent,this);
if (isBidiEnabled)
input_bidi_onfocus(eventOrComponent, this);
this.select();
this.setAttribute("prekeyvalue",this.value);
break;
case "keydown":
this.setAttribute("keydown","true");
if(!ro)
{
lastKeyPress = eventOrComponent.keyCode;
if(eventOrComponent.ctrlKey && hasKeyCode(eventOrComponent,'KEYCODE_SPACEBAR')) {
stopBubble(eventOrComponent);
eventOrComponent.cancelBubble=true;
eventOrComponent.returnValue=false;
break;
}
if(isBidiEnabled)
processBackspaceDelete(eventOrComponent,this);
if(hasKeyCode(eventOrComponent, 'KEYCODE_DELETE') || hasKeyCode(eventOrComponent, 'KEYCODE_BACKSPACE'))
{
getHiddenForm().elements.namedItem("changedcomponentvalue").value = this.value;
}
if((hasKeyCode(eventOrComponent, 'KEYCODE_TAB') || hasKeyCode(eventOrComponent, 'KEYCODE_ESC')))
{
var taMatch = dojo.attr(this, "ta_match");
if(taMatch) {
if(taMatch.toLowerCase().indexOf(this.value.toLowerCase()) == 0)
{
console.log("tamatch="+taMatch);
this.value = taMatch;
input_keydown(eventOrComponent, this);
dojo.attr(this, {"prekeyvalue" : ""});
input_forceChanged(this);
inputchanged = false;
return; // don't want to do input_keydown again so preKeyValue will work
}
}
if(this.getAttribute("PopupType"))
{
var popup = dijit.byId(dojohelper.getPopupId(this));
if (popup)
{
dojohelper.closePickerPopup(popup);
if(hasKeyCode(eventOrComponent, 'KEYCODE_ESC'))
{
if (eventOrComponent.preventDefault)
{
eventOrComponent.preventDefault();
}
else
{
eventOrComponent.returnValue = false;
}
return;
}
}
}
}
input_keydown(eventOrComponent,this);
datespin(eventOrComponent,this);
}
else if(hasKeyCode(eventOrComponent,'KEYCODE_ENTER') || (hasKeyCode(eventOrComponent,'KEYCODE_DOWN_ARROW') && this.getAttribute("liclick")))
{
var lbId = this.getAttribute("li");
frontEndEvent(getElement(lbId), 'click');
}
else if(hasKeyCode(eventOrComponent,KEYCODE_BACKSPACE))
{
eventOrComponent.cancelBubble=true;
eventOrComponent.returnValue=false;
}
break;
case "keypress":
if(!ro)
{
lastKeyPress = eventOrComponent.keyCode;
if(eventOrComponent.ctrlKey==false && hasKeyCode(eventOrComponent,'KEYCODE_ENTER'))
{
var db = this.getAttribute("db");
if(db && db!="")
{
input_forceChanged(this);
sendClick(db);
// IV32363 - When default button is a new row button, then we must set focus on
// the button or the value in text box will be copied to the new row
if (db)
{
window.setTimeout("focusElement(document.getElementById('"+db+"'))", 10);
}
}
}
}
break;
case "keyup":
var keyDown = this.getAttribute("keydown");
this.setAttribute("keydown","false");
if(eventOrComponent.ctrlKey && hasKeyCode(eventOrComponent,'KEYCODE_SPACEBAR'))
{
if(showFieldError(eventOrComponent,this,true))
{
return;
}
else
{
menus.typeAhead(this,0);
}
}
if(!ro)
{
if(isBidiEnabled)
processBidiKeys(eventOrComponent,this);
numericcheck(eventOrComponent,this);
var min = this.getAttribute("min");
var max = this.getAttribute("max");
if(min && max && min!="NONE" || max!="NONE")
{
if(min!="NONE" && parseInt(this.value)<parseInt(min))
{
this.value=min;
getHiddenForm().elements.namedItem("changedcomponentvalue").value = this.value;
this.select();
return false;
}
if(max!="NONE" && parseInt(this.value)>parseInt(max))
{
this.value=max;
getHiddenForm().elements.namedItem("changedcomponentvalue").value = this.value;
this.select();
return false;
}
}
var defaultButton = false;
if(eventOrComponent.ctrlKey==false && hasKeyCode(eventOrComponent,'KEYCODE_ENTER'))
{
var db = this.getAttribute("db");
if(db && db!="")
{
defaultButton=true;
}
}
input_changed(eventOrComponent,this);
}
else
{
setFocusId(eventOrComponent,this);
}
if(showFieldHelp(eventOrComponent, this))
{
return;
}
if(keyDown=="true" && hasKeyCode(eventOrComponent, 'KEYCODE_ENTER') && !eventOrComponent.ctrlKey && !eventOrComponent.altKey)
{
menus.typeAhead(this,0);
return;
}
if(!hasKeyCode(eventOrComponent, 'KEYCODE_ENTER|KEYCODE_SHIFT|KEYCODE_CTRL|KEYCODE_ESC|KEYCODE_ALT|KEYCODE_TAB|KEYCODE_END|KEYCODE_HOME|KEYCODE_RIGHT_ARROW|KEYCODE_LEFT_ARROW')
&& !eventOrComponent.ctrlKey && !eventOrComponent.altKey)
{
menus.typeAhead(this,0);
}
break;
case "mousemove":
overError(eventOrComponent,this);
break;
case "drop":
input_onfocus(eventOrComponent,this);
if (isBidiEnabled)
{
input_bidi_onfocus(eventOrComponent, this);
}
this.select();
if(!ro)
{
this.setAttribute("prekeyvalue",this.value);
}
case "cut":
case "paste":
if(!ro)
{
var fldInfo = this.getAttribute("fldInfo");
if(fldInfo)
{
fldInfo = dojo.fromJson(fldInfo);
if(!fldInfo.query || fldInfo.query!=true)
{
setButtonEnabled(saveButton,true);
}
}
window.setTimeout("inputchanged=true;input_forceChanged(dojo.byId('"+this.id+"'));", 20);
}
break;
}
}
*隐藏表格*
亲爱的@Zhi Lv-MSFT!我尝试了sendkeys方法,但没有成功!我在此link发现了相同的问题。我想在maximo网站上更改值的方法是提取出来的,具有隐藏的形式,但是在那个链接上他们使用了javascript,不要使用IE Automation vba excel。当我检查输入单元格并键入document.getelementbyid("hiddenform")
时,发现了这个隐藏的形式: >
<form aria-hidden="true" id="hiddenform" name="hiddenform" method="POST" action="http://maximo.mysite.com/maximo/ui/maximo.jsp" style="padding:3px;">
<input type="text" size="45" name="event" id="event" title="event type"><br>
<input type="text" size="45" name="targetid" id="targetid" title="target id"><br>
<input type="hidden" size="45" name="value" id="value" value=""><!-- this must be type hidden to support \n in the value -->
<input type="text" size="45" name="changedcomponentid" id="changedcomponentid" title="changed component id"><br>
<input type="text" size="45" name="vischangedcomponentvalue" id="vischangedcomponentvalue" title="changed component value"><br>
<input type="hidden" name="changedcomponentvalue" value="">
<input type="text" size="45" name="currentfocus" id="currentfocus" title="focus id"><br>
<input name="scrollleftpos" id="scrollleftpos" size="45" title="Scroll Left"><br>
<input name="scrolltoppos" id="scrolltoppos" size="45" title="Scroll Top"><br>
<input type="text" size="45" name="uisessionid" id="uisessionid" value="4770" class="fld_ro" readonly="readonly" title="ui session id"><br>
<input type="text" size="45" name="csrftokenholder" id="csrftokenholder" value="ocvdjoiuq2ht8detn26pkjeekg" title="CSRF Token" readonly="readonly" class="fld_ro"><br>
</form>
谢谢!
答案 0 :(得分:0)
能否请您检查是否可以直接通过前端输入值,还是应该执行其他操作(例如选项卡)或从下拉菜单中选择值?通过诸如angular的框架进行的应用程序自动化可能很棘手。
答案 1 :(得分:0)
我确定输入字段中有事件。类似于 onChange(), onFocus(), onBlur()和/或其他。如果在打开的页面的选项卡上按F12键,可以找到它。跟踪DOM检查器的树。在代码段所在的行中,将有一个按钮,在行尾带有标题“事件”。单击该按钮,您可以看到有哪些事件。
过去,可以使用 fireEvent()触发事件。另一种方法是 dispatchEvent()和其他一些方法,例如initEvent()。
[]之间的单词“已过时” ;-) [我过去尝试过的所有方法都无济于事。所以我无法告诉您如何解决您的问题,但我确定这是一个事件问题。]
此链接向您显示其工作方式: Vba, HTMLSelect: FireEvent OnChange or DispatchEvent
[[]]之间的单词与IE11不相关 [[另一种更现代的方式是* addEventListener()*。但是我不知道IE11使用哪种方法: EventTarget.addEventListener() * addEventListener()*对我而言,问题是IE没有eventTarget()构造函数(请参阅页面底部的浏览器兼容性表): EventTarget() ]]
编辑1:
在IE11中,它可以与 dispatchEvent()和其他相关内容一起使用。您必须将所有命令放在自己的方法中。两个例子:
对于 onChange()使用:
Private Sub Event_onChange(htmlDocument As Object, htmlElementWithEvent As Object)
Dim Event_onChange As Object
htmlElementWithEvent.Focus
Set Event_onChange = htmlDocument.createEvent("HTMLEvents")
Event_onChange.initEvent "change", True, False
htmlElementWithEvent.dispatchEvent Event_onChange
End Sub
对于 onKeyDown()使用:
Private Sub Event_onKeyDown(htmlDocument As Object, htmlElementWithEvent As Object)
Dim Event_onKeyDown As Object
htmlElementWithEvent.Focus
Set Event_onKeyDown = htmlDocument.createEvent("HTMLEvents")
Event_onKeyDown.initEvent "onkeydown", True, False
htmlElementWithEvent.dispatchEvent Event_onKeyDown
End Sub
我现在知道这一点,因为我们现在在德语论坛中对此进行讨论。具有自己的sub()的解决方案来自用户Anton(未参与讨论)。现在,借助Antons sub(),我可以弄清楚德国邮政如何让宏在订单空白中插入地址。
德语讨论: Webformular (DHL) ausfüllen
或在存档中一周后: Webformular (DHL) ausfüllen (archive thread)
编辑2:
我认为您必须触发onKeyDown事件。请尝试以下操作:
Dim nodeInput As Object
Set nodeInput = ie.Document.getelementbyid("mx7125[R:0]")
Call TriggerEvent(ie.Document, nodeInput, "onKeyDown")
nodeInput.Value = "Good"
以下是触发事件的函数,例如QHarr在注释中编写的事件类型为第三个函数参数:
Private Sub TriggerEvent(htmlDocument As Object, htmlElementWithEvent As Object, eventType As String)
Dim theEvent As Object
htmlElementWithEvent.Focus
Set theEvent = htmlDocument.createEvent("HTMLEvents")
theEvent.initEvent eventType, True, False
htmlElementWithEvent.dispatchEvent theEvent
End Sub
在上面链接的德语论坛中,“德国邮政”表单现在可以使用此功能。但是有必要两次输入所有值。
我们对您的页面一无所知。该网址似乎是个秘密。如果您需要更多帮助,我们需要更多信息。
答案 2 :(得分:0)
单击“保存”按钮时如何获得输入的文本值?我使用以下代码创建了一个示例,它对我而言效果很好。请参考。
IE.Document.getelementbyid("mx7125[R:0]").Value = "Good"
和以下网页资源(使用javascript脚本获取值document.getElementById('mx7125[R:0]').value
):
<div>
<input aria-labelledby="" id="mx7125[R:0]" class="fld text tt" ctype="textbox" li="mx7126[R:0]"
db="mx5134" maxlength="200" style="width:300px;" ontr='true' async='1' ae="setvalue" type="text"
title="Remark: 11_now_test" value="11_now_test" ov="11_now_test" work="1" fldInfo='{"length":"200","inttype":"0"}' />
</div>
<div aria-live="polite" id="mx7126[R:0]_holder" class="bc">
<img sf="1" aria-hidden="true" active="0" ctype="image" alt="" src="../webclient/skins/skins-20171010-1330/tivoli09/images/blank.gif" source="blank" imgtype=".gif"
style="display:inline;margin:0px;" border="0" lc="mx7125[R:0]" align="absmiddle" width='0px' height='26px' id="mx7126[R:0]" title="" /></div>
<input id="btnSubmit" type="button" value="Submit" onclick="Javascript: alert(document.getElementById('mx7125[R:0]').value);" />
如果仍然无法使用,请发布有关“保存”按钮的相关代码。