PowerShell小技巧实现IE Web自动化
Windows 系统自带的Internet Explore +加上PowerShell 即可搞定。
今天就分享下这几天自己写的几个小函数,欢迎拍砖:
# # 打开IE窗口 # function New-IEWindow { param( [string]$Url, [switch]$Visible, [switch]$FullScreen ) $Global:IEHost = new-object -com "InternetExplorer.Application" $Global:IEHost.Navigate($Url) #设置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 } } # # 根据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) } } # #关闭IE窗口 # function Close-IEWindow { $Global:IEHost.quit() Remove-Variable IEHost -Force } # #设置IE的地址 # function Navigate-IE($Url) { Set-IE -URL $Url } # # 设置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() } } # 设置IE地址 if( $URL) { $Global:IEHost.Navigate($URL) } } # # 在IE窗口中执行脚本 # function Invoke-IEScript($Code,$Language='Javascript') { if( -not [string]::IsNullOrWhiteSpace($Code)) { $Global:IEHost.Document.parentWindow.execScript($Code,$Language) } }
相关推荐
80437916 2020-04-11
WinerChopin 2020-03-03
程序员俱乐部 2020-02-21
cfang00 2013-05-15
流年浅滩 2015-06-14
MaureenChen 2014-05-28
囧芝麻 2019-12-28
jjddrushi 2019-12-27
Safari浏览器 2019-12-23
Safari浏览器 2019-12-11
letheashura 2019-12-03
宇智波鼬 2014-01-20
89357940 2014-01-14
Teresasmida 2014-01-10
Congpanpan 2013-09-04
zljiaa 2019-11-19