更新时间:2023-11-06 12:29:10
以下是我的工作方式。
步骤1 - 确定仅在页面完全呈现后才出现的网页元素。我使用Chrome开发人员工具'Elements'视图显示了DOM视图。
Step 1 - Identify a web page element that only appears once the page is fully rendered. I did this using the Chrome developer tools 'Elements' view which shows the DOM view.
步骤2 - 在脚本中建立一个等待循环,轮询是否存在元素或该元素内的文本值。
Step 2 - Establish a wait loop in the script which polls for the existence of the element or the value of text inside that element.
# Element ID to check for in DOM
$elementID = "systemmessage"
# Text to match in elemend
$elementMatchText = "logged in"
# Timeout
$timeoutMilliseconds = 5000
$ie = New-Object -ComObject "InternetExplorer.Application"
# optional
$ie.Visible = $true
$ie.Navigate2("http://somewebpage")
$timeStart = Get-Date
$exitFlag = $false
do {
sleep -milliseconds 100
if ( $ie.ReadyState -eq 4 ) {
$elementText = (($ie.Document).getElementByID($elementID )).innerText
$elementMatch = $elementText -match $elementMatchText
if ( $elementMatch ) { $loadTime = (Get-Date).subtract($timeStart) }
}
$timeout = ((Get-Date).subtract($timeStart)).TotalMilliseconds -gt $timeoutMilliseconds
$exitFlag = $elementMatch -or $timeout
} until ( $exitFlag )
Write-Host "Match element found: $elementMatch"
Write-Host "Timeout: $timeout"
Write-Host "Load Time: $loadTime"