使用 internetexplorer 对象等待 ajax 响应的正确方法是什么? [英] Using internetexplorer object what is the correct way to wait for an ajax response?

查看:21
本文介绍了使用 internetexplorer 对象等待 ajax 响应的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将文件上传到共享点库,我的代码无法正确检测 ie 是否仍在等待 ajax 响应.这样做的正确方法是什么?

I tried to upload a file to a sharepoint library, my code fails to properly detect if ie is still waiting for an ajax response or not. What is the proper way to do this ?

[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

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

the rest of the code is executed, but the uploading form is not shown yet. How to wait for the display of the form ?

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

I also tried this (should wait untill a button of the upload form is not find), but it never ends ...

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

<小时>

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


Update : I think I've found the problem : the form is open in an 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>

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

But now, How to get the good frame number ?

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

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

moreover it seems i can access the frame content with getElementsByTagName, but not with getElementById ....?I still don't understand why .:

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,选择具有正确位置的 iframe

ok here is how I've done : the trick was to look each iframes, select the one with the correct location

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
}

这篇关于使用 internetexplorer 对象等待 ajax 响应的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆