使用PowerShell向下滚动网页 / PowerShell屏幕截图

6

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

实际上,我正在制作一个脚本,它将自行进行网络报告并给我所需的截图。我可以使用我的脚本打开网页并开始测试,但我希望我的脚本能够向下滚动,以便可以拍到正确的截图。请帮忙。

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

Q)如何在PS中滚动网页?我打开了网站,我想往下滚动?

Q)如何仅截取特定区域的截图?(经过一些研究,我找到了可以截取整个桌面的部分)

我已经附上了我需要的准确截图。

显示OP想要截图外观的图像

脚本从这里开始:

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

1
为什么不使用像PhantomJS这样的适当工具来从网页中获取截图呢? - Tomalak
以前从未尝试过。此外,我熟悉的唯一工具(语言)是Powershell。 - user3421341
当然,用Powershell解决这个问题也没有错。我只是想指出已经存在的工具可能值得调查一下。 - Tomalak
谢谢你让我知道这件事,但我们该如何开始使用PowerShell呢!我甚至不知道该怎么想。 - user3421341
这个也是,为什么除了PowerShell标签之外还有其他所有标签?它们似乎并不相关... - AlphaModder
2个回答

3
该 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}})

3

我想你需要一个非常快速和简单的解决方案。如果是这样,并且您不介意一些丑陋,可以尝试使用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说你需要在DOWN 10周围加上两个波浪括号--{{DOWN 10}}。上面的版本在我写作时肯定可以直接工作,但是可能会因人而异。

担心时间问题最终会回到使用其他工具。你必须采取多少个这样的操作?

请注意,在测试时我已将URL更改为espn.com。不确定您这边情况如何--速度测试?似乎加载了三个不同的页面。


非常感谢您的帮助!我刚意识到使用PowerShell并不是这项工作的明智和可靠选择。我将不得不想出其他办法。 - user3421341
值得一提的是,说PowerShell不是正确的工具有点像说.NET不是正确的工具,因为你可以在PS中几乎做任何你想做的事情,但需要进行一些研究。话虽如此,@Tomalak 提到的PhantomJS看起来确实很不错。 - ruffin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接