Powershell向下滚动网页/ Powershell屏幕截图

时间:2014-10-23 18:55:07

标签: javascript jquery html css powershell

如何在PowerShell中打开网页并向下滚动。

实际上我正在制作一个脚本,它会自己做一个网络报告,并给我一个我想要的截图。我可以打开网页并使用我的脚本开始测试,但我希望我的脚本向下滚动,以便可以拍摄正确的屏幕截图。请帮助。

确切地说,我希望我的脚本打开一个名为testmy.net的网站并进行网络报告。我想截取报告的截图并裁剪其他所有内容。我真的很感激任何帮助。

问)如何在PS中向下滚动网页?我打开网站,我想向下滚动?

问)如何截取特定内容? (经过一些研究后,我得到了一些可以截取整个桌面的截图)

我附上了我需要的确切内容的截图。

An image showing what the OP wants his screenshot to look like

脚本从这里开始:

$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"

2 个答案:

答案 0 :(得分:3)

我认为你正在寻找真的快速而肮脏。如果这是真的,你不介意丑陋,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}

害怕你会有足够的计时问题,你最终会回来并使用其他工具。你必须采取多少这些?

请注意,我在测试时确实将网址更改为espn.com。不确定你的是什么 - 速度测试?似乎加载了大约三个不同的页面。

答案 1 :(得分:2)

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)

这是比sendkeys更加可控和可重复的方法。使用SendKeys时,滚动的像素数量取决于窗口大小,在此代码中,您可以滚动绝对像素数,而不管窗口大小。

我的代码也使用了答案中的ConnectIExplorer函数:

PowerShell IE9 ComObject has all null properties after navigating to webpage

这解决了在没有提升权限的情况下运行脚本时保护模式干扰IE Com对象脚本的问题。

为方便起见,用户@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}})