使用InternetExplorer对象是什么等待Ajax响应的正确方法?对象、正确、方法、InternetExplorer

2023-09-10 14:49:50 作者:旧伤 -Too ha

我试图将文件上载到SharePoint库,我的code未能正确检测如果IE仍在等待Ajax响应与否。什么是正确的方式做到这一点?

  [空] [System.Reflection.Assembly] :: LoadWithPartialName('Microsoft.VisualBasic程序)
[空] [System.Reflection.Assembly] :: LoadWithPartialName('System.Windows.Forms的)

功能wait4IE($ IE = $全球:IE){
    而($ ie.busy  - 或$ ie.readystate -lt 4){启动睡眠-milliseconds 200}
}

$全球:即=新的对象-cominternetexplorer.application
$ ie.visible = $真
[Microsoft.VisualBasic.Interaction] ::则AppActivate(Internet Explorer的)

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

#点击按钮来显示表单
$ ie.Document.getElementById(QCB1_Button2)。点击()

wait4IE
 

执行的code休息,但上载的形式是没有表现出。 如何等待形式的显示?

我也试过这个(应等待上传表单中,直到一个按钮没有找到),但它永远不会结束......

 而($ ie.document.getElementById(ctl00_PlaceHolderMain_UploadDocumentSection_ctl05_InputFile)当量$空){
        回声等......
        wait4IE
}
 

更新: 我想我已经找到了问题所在:表格中的iframe是开放的:

 < IFRAME ID =DlgFrame0be35d71-22cb-47bd-bbf0-44c97db61fd6级=MS-dlgFrame src="https://.../Upload.aspx?List={45085FA0-3AE3-4410-88AD-3E80A218FC0C}&RootFolder=&IsDlg=1" FRAMEBORDER =0的风格=宽度:592px;高度:335px;>< / IFRAME>
 
internet explorer脚本错误对象不支持此属性或方法

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

  PS>?($ ie.Document.frames.Item(4).document.body.getElementsbytagname(输入)| {$ _输入-eq'文件'} ).ID
ctl00_PlaceHolderMain_UploadDocumentSection_ctl05_InputFile
 

此外,它似乎我可以访问的框架内容的getElementsByTagName,但不与的getElementById ....我还是不明白,为什么?

PS>$ie.Document.frames.Item(4).document.body.getElementById('ctl00_PlaceHolderMain_UploadDocumentSection_ctl05_InputFile     ')     ÉchecLORS DE L'APPEL德拉METHODE,汽车[系统.__ ComObject]东北contient加莱METHODEnommée«的getElementById»。     金caractère利涅:1:1     + $ ie.Document.frames.Item(4).document.body.getElementById('ctl00_PlaceHolderMain_ ...     + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~         + CategoryInfo:InvalidOperation:(的getElementById:字符串)[],RuntimeException的         + FullyQualifiedErrorId:MethodNotFound

解决方案

确定这里就是我所做的: 诀窍是寻找每一个内部框架,选择一个具有正确的位置

 为($ i = 0; $ I $ -lt ie.Document.frames.length; $ I ++){
            如果($ ie.Document.frames.item($ I).location.href -matchupload.aspx'){$ FRM = $ ie.Document.frames.item($ I)}
    }
 

然后等待我的输入显示

 ,而(($ frm.document.body.getElementsbytagname(输入)|?{$ _输入-eq'文件'})当量$空){
    回声等......
    启动睡眠-milliseconds 100
}
 

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
}

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

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

解决方案

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)}
    }

then wait for my input to show

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