如何在powershell中打开网页并向下滚动。
实际上,我正在制作一个脚本,它将自己做一个网络报告,并给我一个我想要的屏幕截图。我可以打开网页并用我的脚本开始测试,但我希望我的脚本向下滚动,以便可以拍摄正确的屏幕截图。请帮帮忙。
准确地说,我希望我的脚本打开一个名为testmy.net的网站,并做一个网络报告。我只想截取报告的屏幕截图,然后裁剪其他所有内容。我真的很感谢任何人的帮助。
Q)如何在PS中向下滚动网页?我打开网站,想要向下滚动吗?
问:如何截取特定事物的屏幕截图?(经过一些研究,我得到了一些可以截取整个桌面的截图的部分)
我已经附上了我需要的东西截图。
脚本从这里开始:
$ie = new-object -comobject InternetExplorer.Application -property `
@{navigate2="http://testmy.net/SmarTest/combinedAuto"; visible = $true}
# Wait for the page to finish loading
$ie.fullscreen = $true
do {sleep 5} until (-not ($ie.Busy))
# Take A ScreenShot (Script taken from Stackflow)
[Reflection.Assembly]::LoadWithPartialName("System.Drawing")
function screenshot([Drawing.Rectangle]$bounds, $path) {
$bmp = New-Object Drawing.Bitmap $bounds.width, $bounds.height
$graphics = [Drawing.Graphics]::FromImage($bmp)
$graphics.CopyFromScreen($bounds.Location, [Drawing.Point]::Empty, $bounds.size)
$bmp.Save($path)
$graphics.Dispose()
$bmp.Dispose()
}
$bounds = [Drawing.Rectangle]::FromLTRB(0, 0, 1000, 900)
screenshot $bounds "C:\screenshot.png"
发布于 2014-10-24 06:52:17
我想你要找的是又快又脏的。如果那是真的,而且你不介意丑陋,try using SendKeys。
$ie = new-object -comobject InternetExplorer.Application -property `
@{navigate2="http://testmy.net/SmarTest/combinedAuto"; visible = $true}
# Wait for the page to finish loading
$ie.fullscreen = $true
do {sleep 5} until (-not ($ie.Busy))
[System.Windows.Forms.Cursor]::Position = New-Object system.drawing.point(700,700)
[System.Windows.Forms.SendKeys]::SendWait({DOWN 10})
# Take A ScreenShot (Script taken from Stackflow)
[Reflection.Assembly]::LoadWithPartialName("System.Drawing")
function screenshot([Drawing.Rectangle]$bounds, $path) {
$bmp = New-Object Drawing.Bitmap $bounds.width, $bounds.height
$graphics = [Drawing.Graphics]::FromImage($bmp)
$graphics.CopyFromScreen($bounds.Location, [Drawing.Point]::Empty, $bounds.size)
$bmp.Save($path)
$graphics.Dispose()
$bmp.Dispose()
}
$bounds = [Drawing.Rectangle]::FromLTRB(0, 0, 1000, 900)
screenshot $bounds "C:\tmp\screenshot.png"
不断修改您发送的向下箭头的数量,直到它是正确的--所以编辑{DOWN 10}
。
注意: Chirishman says,你需要在
DOWN 10
--{{DOWN 10}}
周围有两个小括号。在我写这篇文章的时候,上面的版本几乎完全可以在我的机器上运行,但是ymmv。
然而,担心你会有足够的时间问题,以至于你最终会回去使用另一个工具。你要吃多少?
请注意,我在测试时确实将URL更改为espn.com。不知道你那里发生了什么--速度测试?似乎加载了三个不同的页面。
发布于 2016-08-25 05:02:53
COM对象实际上有一个滚动控件
$HWND = ($objIE = New-Object -ComObject InternetExplorer.Application).HWND
[int]$targetHorizontalScroll = 0
[int]$targetVerticalScroll = 100
[string]$uri = "https://www.test.com"
$objIE.Navigate($uri)
[sfw]::SetForegroundWindow($HWND) | Out-Null
#Omit the next line if running as administrator. Else, see below comment for a link
$objIE = ConnectIExplorer -HWND $HWND
$objIE.Document.parentWindow.scroll($targetHorizontalScroll,$targetVerticalScroll)
这是一种比sendkey更具可控性和可重复性的方法。使用SendKeys时,滚动的像素数取决于窗口大小,在此代码中,无论窗口大小如何,滚动的像素数都是绝对值。
我的代码还使用了下面答案中的ConnectIExplorer函数:
PowerShell IE9 ComObject has all null properties after navigating to webpage
修复了在没有提升权限的情况下运行脚本时,保护模式会干扰IE Com对象的脚本编写的问题。
为方便起见,user @bnu的函数也在此处重现:
function ConnectIExplorer() {
param($HWND)
$objShellApp = New-Object -ComObject Shell.Application
try {
$EA = $ErrorActionPreference; $ErrorActionPreference = 'Stop'
$objNewIE = $objShellApp.Windows() | ?{$_.HWND -eq $HWND}
$objNewIE.Visible = $true
} catch {
#it may happen, that the Shell.Application does not find the window in a timely-manner, therefore quick-sleep and try again
Write-Host "Waiting for page to be loaded ..."
Start-Sleep -Milliseconds 500
try {
$objNewIE = $objShellApp.Windows() | ?{$_.HWND -eq $HWND}
$objNewIE.Visible = $true
} catch {
Write-Host "Could not retreive the -com Object InternetExplorer. Aborting." -ForegroundColor Red
$objNewIE = $null
}
} finally {
$ErrorActionPreference = $EA
$objShellApp = $null
}
return $objNewIE
}
同样值得注意的是,@ruffin的答案包含一个错误。正如所写的那样,它将输入"DOWN 10“,而不是发送十次向下箭头(并意外地向下滚动,因为它包括一个空格键按下)。这可以用第二组花括号来修复,如下所示:
[System.Windows.Forms.SendKeys]::SendWait({{DOWN 10}})
https://stackoverflow.com/questions/26535311
复制相似问题