powershell可以等到IE准备好DOM吗? [英] Can powershell wait until IE is DOM ready?

查看:138
本文介绍了powershell可以等到IE准备好DOM吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本,我正在使用

I'm currently writing a script and I'm using

while($IE.busy) {Start-Sleep 1}

等待页面准备就绪。

页面准备好后,我的脚本会填写并提交表单。我一直遇到问题,因为(我认为)IE报告说它是在页面加载时但在文档被渲染之前完成的(这会导致脚本错误)。我目前的解决方法是在页面加载后添加4秒的等待时间,但如果可能的话,我会觉得使用非基于时间的方法会更舒服。

Once the page is ready my script fills out and submits a form. I've been running into problems because (I think) IE reports that it's done when the page is loaded but before the document has been rendered (which causes the script to error out). My current workaround is to add a 4 second wait time after the page loads but I'd feel more comfortable with a method that isn't time based if possible.

推荐答案

以下是我的工作方式。

步骤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"

这篇关于powershell可以等到IE准备好DOM吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆