Using internetexplorer object what is the correct

2019-03-05 11:11发布

问题:

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

回答1:

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
}