Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Powershell向下滚动网页/Powershell屏幕截图_Javascript_Jquery_Html_Css_Powershell - Fatal编程技术网

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

Javascript Powershell向下滚动网页/Powershell屏幕截图,javascript,jquery,html,css,powershell,Javascript,Jquery,Html,Css,Powershell,如何在powershell中打开网页并向下滚动 实际上,我正在制作一个脚本,它将自己制作一个网络报告,并给我一个我想要的屏幕截图。我可以打开网页,用我的脚本开始测试,但我希望我的脚本向下滚动,以便拍摄正确的屏幕截图。请帮忙 确切地说,我希望我的脚本打开一个名为testmy.net的网站,并做一个网络报告。我只想截取报告的屏幕截图,然后裁剪其他所有内容。我真的很感激任何帮助 Q) 如何在PS中向下滚动网页?我打开网站,我想向下滚动 Q) 如何只拍摄特定事物的屏幕截图? (经过一些研究,我得到了一些

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

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

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

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

Q) 如何只拍摄特定事物的屏幕截图? (经过一些研究,我得到了一些可以截图整个桌面的部件)

我已经附上了截图的确切的东西,我需要

脚本从这里开始:

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

我想你是在找又快又脏的东西。如果这是真的,你不介意丑陋

继续摆弄你发送的下箭头的数量,直到它正确为止——所以编辑
{down 10}

害怕你会有足够的时间问题,你最终会回去使用另一个工具,然而。你要吃多少


请注意,我在测试时确实将URL更改为espn.com。不知道你的车出了什么事——速度测试?似乎加载了大约三个不同的页面。

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时,您滚动的像素量取决于窗口大小,在此代码中,您滚动的像素绝对数与窗口大小无关

我的代码还使用了下面答案中的ConnectionExplorer函数:

它修复了当脚本在没有提升权限的情况下运行时,受保护模式干扰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}})

为什么不使用像PhantomJS这样合适的工具从网页截图呢?以前从未尝试过。此外,我唯一熟悉的工具(语言)是Powershell。当然,在Powershell中解决它没有什么错。我只是想指出,工具已经存在,可能值得研究。感谢您让我了解它,但我们怎么能从powershell开始呢!我也不知道该怎么想,为什么除了powershell还有其他标签?他们似乎关系不大……非常感谢您的帮助,先生!我刚刚意识到,对于这项工作来说,使用powershell不是一个明智和可靠的选择。我还得想点别的。说Powershell不是合适的工具就好像说.NET不是合适的工具一样,这是值得的,因为你可以在PS中用.NET做任何你想做的事情,但这需要一些研究。也就是说,@Tomalak的建议看起来很好。
[System.Windows.Forms.SendKeys]::SendWait({{DOWN 10}})