Vba 如何更新动态元素字段(输入ID和名称)

Vba 如何更新动态元素字段(输入ID和名称),vba,internet-explorer,Vba,Internet Explorer,我正在尝试更新包含以下元素的字段的金额。我不确定要使用什么代码作为输入id和名称,因为它们不是固定的,并且每次访问时都会更改。它没有给我任何错误消息,但在我运行代码后,该字段没有更新 `<div class="field-item dataValueWrite"> <table> <tbody> <tr> <td nowrap="" align="left"> <div id="93497E

我正在尝试更新包含以下元素的字段的金额。我不确定要使用什么代码作为输入id和名称,因为它们不是固定的,并且每次访问时都会更改。它没有给我任何错误消息,但在我运行代码后,该字段没有更新

`<div class="field-item dataValueWrite">
  <table>
   <tbody>
    <tr>
     <td nowrap="" align="left">
      <div id="93497ErrorCUSTOM" class="" style="display:none;"><span class="" errid="" title="Please enter a valid amount" id="93497PegaRULESErrorFlag"></span></div>
      <input id="93497" name="93497" onchange="changedInputAmountInCurrencyDisplay(this , '2' , '.' , '.' , ',' , 'false' ); " class="rightJustifyStyle" type="text" size="10" value="" data-changed="false"><script> formatCurrencyBeforeLoad(document.getElementById('93497'),'2','.', 'false'); 
      </script><input id="93497HIDDEN" name="$PAcqCaseCreation$pMessageAmountUSD" type="hidden" value="0.00">
      <script> 
         formatCurrencyBeforeLoad(document.getElementById('93497HIDDEN'),'2', '.' , 'false'); 
      </script> 
        &nbsp;USD  
     </td>
    </tr>
   </tbody>
  </table>
 </div>`
`
formatCurrencyBeforeLoad(document.getElementById('93497'),'2','false');
formatCurrencyBeforeLoad(document.getElementById('93497HIDDEN')、'2','false');
美元
`
以下是我迄今为止创建的代码:

Sub Test()
Dim IE As Object
Set IE = New InternetExplorerMedium
Dim doc As HTMLDocument

IE.navigate "intranet"
IE.Visible = True

While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE: DoEvents: Wend

Set doc = IE.document

Dim i As Integer
Dim lastRow As Integer
Dim fileLink26 As String
Dim fileLink19 As String

Sheets("Case").Select
lastRow = Sheets("Case").Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow

doc.contentDocument.querySelector("[onclick='CreateAcqCase();']").Click

Application.Wait (Now + TimeValue("00:00:05"))

fileLink26 = Cells(i, 26)
fileLink19 = Cells(i, 19)

    If fileLink26 = "" And fileLink19 = "CB" And Cells(i, 10).Value = "USD" Then
    On Error Resume Next
    Dim DispStg2 As Object
    Do While DispStg2 Is Nothing
    Set DispStg2 = doc.getElementsByTagName("iframe")(0).contentDocument.querySelector("input[value='FirstCB']")
    DoEvents
    Loop
    DispStg2.Click

Application.Wait (Now + TimeValue("00:00:03"))

doc.getElementsByTagName("iframe")(0).contentDocument.querySelector("input.rightJustifyStyle").Item(2).Value = Sheets("Case").Range("k2") '>> This is where I stuck cause nothing happen after this code ran

End If
Next i
End Sub
子测试()
模糊的物体
设置IE=新的InternetExplorerMedium
作为HTMLDocument的Dim doc
浏览“内部网”
可见=真实
当IE.Busy或IE.readyState readyState_完成时:DoEvents:Wend
Set doc=IE.document
作为整数的Dim i
将最后一行设置为整数
Dim fileLink26作为字符串
Dim fileLink19作为字符串
表格(“案例”)。选择
lastRow=工作表(“大小写”)。单元格(Rows.Count,“A”)。结束(xlUp)。行
对于i=2到最后一行
doc.contentDocument.querySelector(“[onclick='CreateAcqCase();']”)。单击
Application.Wait(现在+时间值(“00:00:05”))
fileLink26=单元格(i,26)
fileLink19=单元格(i,19)
如果fileLink26=“”和fileLink19=“CB”和单元格(i,10).Value=“USD”,则
出错时继续下一步
作为对象的Dim DispStg2
DispStg2不起作用时执行
Set DispStg2=doc.getElementsByTagName(“iframe”)(0.contentDocument.querySelector(“输入[value='FirstCB']”)
多芬特
环
DispStg2。单击
Application.Wait(现在+时间值(“00:00:03”))
doc.getElementsByTagName(“iframe”)(0).contentDocument.querySelector(“input.rightJustifyStyle”).Item(2).Value=Sheets(“Case”).Range(“k2”)”>>这就是我卡住的地方,因为在运行此代码后不会发生任何事情
如果结束
接下来我
端接头
从上面的示例代码来看,您似乎正在尝试设置iframe中具有类名rightJustifyStyle的第二个输入元素的值

您可以参考下面的示例

Public Sub demo()
    Dim ie As Object

    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True

        ie.Navigate2 "D:\Backup20190913\tests\354.html"

        While .Busy Or .readyState < 4: DoEvents: Wend

        ie.Navigate2 ie.document.querySelector("iframe").src

        While .Busy Or .readyState < 4: DoEvents: Wend

        ie.document.querySelector("input.rightJustifyStyle:nth-of-type(2)").Value = "Test value..."


        While .Busy Or .readyState < 4: DoEvents: Wend
        Stop
        ie.Quit

End Sub
公共子演示()
模糊的物体
设置ie=CreateObject(“InternetExplorer.Application”)
可见=真实
ie.Navigate2“D:\Backup20190913\tests\354.html”
当.Busy或.readyState<4:DoEvents:Wend时
ie.Navigate2 ie.document.querySelector(“iframe”).src
当.Busy或.readyState<4:DoEvents:Wend时
ie.document.querySelector(“input.rightJustifyStyle:nth of type(2)”).Value=“测试值…”
当.Busy或.readyState<4:DoEvents:Wend时
停止
即退出
端接头
输出:


如果我误解了什么,请告诉我。我将试着纠正自己。

您被卡住的那一行是在Excel工作表中为范围赋值。您的Excel文件是否具有网页中的值?你说在那条线之后什么都没用。我们需要那个不起作用的代码来查看是否有任何东西导致了这个问题。您的代码可能无法在Iframe中找到控件。尝试在控制台中打印该值以进行检查。@Deepak MSFT-除我突出显示的行外,所有代码都已成功运行。顺便说一句,我对该行做了一些修改,因为我的目的是用excel上显示的值更新该字段,但它仍然不起作用。我试图在控制台中打印要检查的值,但结果为空(fac=doc.getElementsByTagName(“iframe”)(0)。contentDocument.querySelector(“input.rightJustifyStyle”)。项(2)。值调试。打印fac)。希望你能帮助我,因为我尝试了几天的代码搜索,但仍然没有结果。是的,你是对的,但运行代码后什么都没有发生。在同一iframe中,还有其他字段需要更新,但除此特定字段外,所有字段都具有固定的元素值。您是否获得了与上述示例类似的结果?是的,我已根据您的示例修改了代码,添加了类型(2)的第n个请尝试在测试中使用ie.Navigate2 ie.document.querySelector(“iframe”).src。我昨天已经尝试过了,但仍然得到了相同的结果。
Public Sub demo()
    Dim ie As Object

    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True

        ie.Navigate2 "D:\Backup20190913\tests\354.html"

        While .Busy Or .readyState < 4: DoEvents: Wend

        ie.Navigate2 ie.document.querySelector("iframe").src

        While .Busy Or .readyState < 4: DoEvents: Wend

        ie.document.querySelector("input.rightJustifyStyle:nth-of-type(2)").Value = "Test value..."


        While .Busy Or .readyState < 4: DoEvents: Wend
        Stop
        ie.Quit

End Sub