我遇到IE不接受我试图输入字段的值(计算机的名称)的问题。我设置了值,它在字段中可见,但当我在表单上单击“提交”时,它(IE)无法识别我输入的值。我可以修改页面上的其他对象,但不能修改这个简单的文本字段。我甚至可以在IE中设置后从对象中检索该值。该字段是必填字段,因此提交例程会在我继续之前失败。
下面是一些代码:
'Find the correct instance of IE
Set objShell = CreateObject("Shell.Application")
' etc.
Set IE = objShell.Windows(x)
' etc.
' Enter Computer_Name
Set objCollection = IE.Document.getElementsByTagName("iframe")(2).contentDocument.getElementById("QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC")
objCollection.Value = Computer_Name ' (The text I'm trying to enter)
' Some other stuff that is working
' Click the "Submit" button on the form (this works too).
单击“提交”后,网页会弹出一个错误,提示未输入必填字段(计算机名称)。该字段以红色突出显示。
当我手动输入计算机名称时,它工作得很好,但使用VBA时就不行了。任何帮助都将不胜感激!
以下是HTML代码的示例。我将高亮显示我试图修改的元素。
<div class="questionContainer ">
<div class="left questionBody" id="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC-label-body" required="false">
<label class="questionlabel bold" id="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC-label" for="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC">Computer Name<span class="required">*</span></label>
</div>
<div class="left answerBody block">
<div class="left">
<div id="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC-answer-body">
' *********** The next line is the element in question. ************
<input tabindex="0" class="answerTextarea " id="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC" aria-describedby="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC-instructions" aria-labelledby="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC-label" required="true" type="text" size="40" value="" responsetype="TEXT" questiondefid="QDHAA5V0GH4LSAO2AI6F2MXNIAJ5CE" totalorder="3" questionid="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC" level="0" overwrite="1" maxlng="16" wrap="virtual">
</div>
</div>
<div class="validationResult clear" id="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC-validationResult" style="display: none;"></div>
<div class="clear"></div>
<div class="instructions" id="QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC-instructions"></div>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
可能重要的注意事项:后面的代码行中显示"style="display: none;的代码位不会出现,直到在IE中手动将该值输入到字段中。
谢谢!
发布于 2018-07-31 23:48:20
感谢Tim Williams的帮助,您的想法使我走上了导致这个解决方案的旅程。事实证明,我需要触发事件才能让页面接受该值。非常简单,当我意识到这一点的时候。下面是我的最终代码:
Dim objEvent ' **** NEW ****
'Find the correct instance of IE (opened manually initially)
Set objShell = CreateObject("Shell.Application")
' etc.
Set IE = objShell.Windows(x)
' etc. (including navigation when necessary)
' After the webpage has loaded, set the event handler ' **** NEW ****
Set objEvent = IE.Document.createEvent("HTMLEvents") ' **** NEW ****
' Enter Computer_Name
Set objCollection = IE.Document.getElementsByTagName("iframe")(2).contentDocument.getElementById("QSHAA5V0GH4LSAO2AI6F2MXNIAJ5CC")
objCollection.Value = Computer_Name ' (The text I'm entering)
' Trigger the event change ' **** NEW ****
objEvent.initEvent "change", False, True ' **** NEW ****
objCollection.dispatchEvent objEvent ' **** NEW ****
' Enter other needed information
' Click the "Submit" button on the form.
https://stackoverflow.com/questions/51566881
复制相似问题