パスワードを忘れた? アカウント作成
258550 journal

black-holeの日記: Powershell入門: ブラウザがWebページを読み込むまで待つ。

日記 by black-hole

Webページの指定したコントロールが有効になるまで待つ。
リトライ回数×待ち時間を超えた場合はエラー。

function waitctrl($browser, $url, $ctrlId, $retryMax, $waitTime) {

        if ( $url -ne $null ) {
                Write-host "Loading $url..."
                $browser.navigate($url)
        }

        $retry = 1

        while ($retry -le $retryMax) {

                Write-host "Waiting for page to load... $retry."

                [System.Threading.Thread]::Sleep($waitTime)

                $doc = $browser.document
                if ($doc -ne $null) {

                        $ctrl = $doc.getElementByID($ctrlId)
                        if ($ctrl -ne $null) {
                                return $true
                        }
                }

                $retry++
        }

        Write-host "Can't load page."

        return $false
}

#
# 使用例
#
$url = # Webページ
$idSaveBottom = # セーブボタンのコントロールId
$ie = new-object -comObject "InternetExplorer.Application"
$ie.visible = $true

# セーブボタンが有効になるまで待つ。
waitctrl $ie $url $idSaveBottom 5 10
if ( -not $? ) {
        # エラー処理
}

# ポチッとな。
$doc = $ie.document
$save = $doc.getElementById($idSaveBottom)
$save.click()

この議論は賞味期限が切れたので、アーカイブ化されています。 新たにコメントを付けることはできません。
typodupeerror

最初のバージョンは常に打ち捨てられる。

読み込み中...