用 PS 操作 IE

来源:互联网 发布:python图形界面 编辑:程序博客网 时间:2024/04/25 17:53
用 PS 操作 IE


一、抑制警告窗口


    $win.execScript("window.alert=function(){}", "javascript")


二、调用 execScript 时出错


使用“2”个参数调用“execScript”时发生异常:“由于出现错误 80020101 而导致此项操作无法完成。”


获得焦点,好像更有利于窗口的打开。


$ie.Document.parentWindow.focus()


三、弹出窗口,且关闭原窗口,使原来的操作句柄失效,必需要找到新的弹出窗口,才能进行下一步的操作。


function returnWindow($s){
    (New-Object -ComObject Shell.Application).Windows() `
        | Where { $_.FullName.toLower().EndsWith("iexplore.exe") } `
        | where { $_.LocationURL -eq $s } 
# $s 为 "弹出窗口的新地址"
    }


四、等待 IE 加载完成


function waitForLoad ($ie) {
    while ( $ie.Busy ){ Start-Sleep -Milliseconds 100}
}


五、PS 2.0 对 mshtml 的封装不完善。对于没有 ID 或没有唯一 name 的元素无法进行操作。升级到 PS 3.0 以后,就没有这个问题了。


$buttons.length 返回空,没有 $buttons.item 属性。


    $doc = $ie.Document


    $buttons = $doc.getElementsByTagName( "input")


    if ($buttons.length -gt 0 ){
        $buttons.item(1).click()
        }


六、在 PS 控制台中运行正常,但是,不能在任务计划中运行。


    $ie.Document.parentWindow.focus()
0 0