black-holeの日記: Powershell入門: ブラウザがWebページを読み込むまで待つ。
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()
Powershell入門: ブラウザがWebページを読み込むまで待つ。 More ログイン