gpt4 book ai didi

excel - VBA Excel 值未在 IE 中记录/接受

转载 作者:行者123 更新时间:2023-12-02 19:06:18 25 4
gpt4 key购买 nike

我遇到了 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 中手动输入该值后才会出现。

谢谢!

最佳答案

感谢蒂姆·威廉姆斯 (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.

关于excel - VBA Excel 值未在 IE 中记录/接受,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51566881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com