#
# 打開IE窗口
#
function New-IEWindow
{
param(
[string]$Url,
[switch]$Visible,
[switch]$FullScreen
)
$Global:IEHost = new-object -com "InternetExplorer.Application"
$Global:IEHost.Navigate($Url)
#設(shè)置IE可見性和全屏
$Global:IEhost.Visible= $Visible
$Global:IEHost.FullScreen= $FullScreen
}
#
#等待IE加載完畢
#
function Wait-IEReady([int]$TimeoutSeconds=10)
{
$milliseconds=0
$maxMilliseconds = $TimeoutSeconds * 1000
while($Global:IEHost.Busy)
{
if($milliseconds -gt $maxMilliseconds)
{
throw 'Wait ie ready timeout.'
}
sleep -Milliseconds 100
$milliseconds+=100
}
}
#
# 根據(jù)ID,Class,Name,Tag獲取HTML元素
#
function Get-HtmlElement ($Id,$Name,$Class,$Tag)
{
if($Id)
{
return $IEHost.Document.getElementById($id)
}
elseif($Name)
{
return $IEHost.Document.getElementsByName($Name)
}
elseif($Class)
{
$IEHost.Document.all | where {$_.className -contains $Class}
}
elseif($Tag)
{
$IEHost.Document.getElementsByTagName($Tag)
}
}
#
#關(guān)閉IE窗口
#
function Close-IEWindow
{
$Global:IEHost.quit()
Remove-Variable IEHost -Force
}
#
#設(shè)置IE的地址
#
function Navigate-IE($Url)
{
Set-IE -URL $Url
}
#
# 設(shè)置IE的地址,或者動作:前進,倒退,刷新
#
function Set-IE
{
param(
[ValidateSet('GoBack', 'GoForward','Refresh')]
[string]$Action,
[uri]$URL
)
# 動作
switch($Action)
{
('GoBack'){ $Global:IEHost.GoBack() }
('GoForward'){ $Global:IEHost.GoForward() }
('Refresh'){ $Global:IEHost.Refresh() }
}
# 設(shè)置IE地址
if( $URL) {
$Global:IEHost.Navigate($URL) }
}
#
# 在IE窗口中執(zhí)行腳本
#
function Invoke-IEScript($Code,$Language='Javascript')
{
if( -not [string]::IsNullOrWhiteSpace($Code))
{
$Global:IEHost.Document.parentWindow.execScript($Code,$Language)
}
}