使用 internetexplorer 对象等待 ajax 响应的正确方法是什么?

Using internetexplorer object what is the correct way to wait for an ajax response?

我试图将文件上传到共享点库,我的代码无法正确检测 ie 是否仍在等待 ajax 响应。执行此操作的正确方法是什么?

[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")

function wait4IE($ie=$global:ie){
    while ($ie.busy -or $ie.readystate -lt 4){start-sleep -milliseconds 200}
}

$global:ie=new-object -com "internetexplorer.application"
$ie.visible=$true
[Microsoft.VisualBasic.Interaction]::AppActivate("internet explorer")

# open EDM
$ie.navigate("https://xxx.sharepoint.com/sites/site1/Forms/AllItems.aspx")
wait4IE

# click on  the button to display the form
$ie.Document.getElementById("QCB1_Button2").click()

wait4IE

其余代码已执行,但上传表单尚未显示。 如何等待表单显示?

我也试过这个(应该等到找不到上传表单的按钮),但它永远不会结束...

while( $ie.document.getElementById("ctl00_PlaceHolderMain_UploadDocumentSection_ctl05_InputFile") -eq $null){
        echo "waiting ..."
        wait4IE
}

更新: 我想我发现了问题:表单在 iframe 中打开:

<iframe id="DlgFrame0be35d71-22cb-47bd-bbf0-44c97db61fd6" class="ms-dlgFrame" src="https://.../Upload.aspx?List={45085FA0-3AE3-4410-88AD-3E80A218FC0C}&amp;RootFolder=&amp;IsDlg=1" frameborder="0" style="width: 592px; height: 335px;"></iframe>

但是现在,如何获得好的帧数?

PS>($ie.Document.frames.Item(4).document.body.getElementsbytagname("input") |?{$_.type -eq 'file'}).id
ctl00_PlaceHolderMain_UploadDocumentSection_ctl05_InputFile

此外,我似乎可以使用 getElementsByTagName 访问框架内容,但不能使用 getElementById ....?我仍然不明白为什么。:

PS>$ie.Document.frames.Item(4).document.body.getElementById('ctl00_PlaceHolderMain_UploadDocumentSection_ctl05_InputFile
    ')
    Échec lors de l'appel de la méthode, car [System.__ComObject] ne contient pas de méthode nommée « getElementById ».
    Au caractère Ligne:1 : 1
    + $ie.Document.frames.Item(4).document.body.getElementById('ctl00_PlaceHolderMain_ ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation : (getElementById:String) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound

好的,这是我的做法: 诀窍是查看每个 iframe,select 位置正确的

for($i=0;$i -lt $ie.Document.frames.length;$i++){
            if( $ie.Document.frames.item($i).location.href -match 'upload.aspx' ){ $frm=$ie.Document.frames.item($i)}
    }

然后等待我的输入显示

while( ($frm.document.body.getElementsbytagname("input") |?{$_.type -eq 'file'}) -eq $null){
    echo "waiting ..."
    start-sleep -milliseconds 100
}