admin管理员组

文章数量:1312778

How to open a webpage in powershell and scroll down.

Actually I am making a script which will do a network report on its own and give me a screenshot which I want. I can open the webpage and start the test with my script, but I want my script to scroll down so that the correct screenshot could be taken. Please Help.

To be precise, I want my script to open a website called testmy and do a network report. I want to take the screenshot of just the report and crop everything else. I would really appreciate any help.

Q) How do I scroll down a webpage in PS? I open the website and I want to scroll down?

Q) How do I take a screenshot of only a particular thing? (After some research I got some part which could take a screenshot of the whole desktop)

I have attached the screenshot of exact thing I need.

Script Starts Here:

$ie = new-object -object InternetExplorer.Application -property `
    @{navigate2=""; 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"

How to open a webpage in powershell and scroll down.

Actually I am making a script which will do a network report on its own and give me a screenshot which I want. I can open the webpage and start the test with my script, but I want my script to scroll down so that the correct screenshot could be taken. Please Help.

To be precise, I want my script to open a website called testmy and do a network report. I want to take the screenshot of just the report and crop everything else. I would really appreciate any help.

Q) How do I scroll down a webpage in PS? I open the website and I want to scroll down?

Q) How do I take a screenshot of only a particular thing? (After some research I got some part which could take a screenshot of the whole desktop)

I have attached the screenshot of exact thing I need.

Script Starts Here:

$ie = new-object -object InternetExplorer.Application -property `
    @{navigate2="http://testmy/SmarTest/binedAuto"; 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"
Share Improve this question edited Oct 23, 2014 at 22:35 ruffin 17.5k10 gold badges96 silver badges149 bronze badges asked Oct 23, 2014 at 18:55 user3421341user3421341 1112 silver badges6 bronze badges 5
  • 1 Why not use something appropriate like PhantomJS to take screenshots from web pages? – Tomalak Commented Oct 23, 2014 at 19:57
  • Never tried it before. Moreover, the only tool (language) I am familiar with is Powershell. – user3421341 Commented Oct 23, 2014 at 21:10
  • Sure, and there's nothing wrong with solving it in Powershell. I'm just pointing out that tools already exist and might be worthwhile investigating. – Tomalak Commented Oct 23, 2014 at 21:12
  • Thanks for letting me know about it, but how could we even start with powershell! I don't even know what to think – user3421341 Commented Oct 23, 2014 at 21:15
  • This one too, why are there all the tags other than powershell? They don't seem very related... – AlphaModder Commented Oct 24, 2014 at 15:21
Add a ment  | 

2 Answers 2

Reset to default 3

The COM object actually has a scroll control

$HWND = ($objIE = New-Object -ComObject InternetExplorer.Application).HWND
[int]$targetHorizontalScroll = 0
[int]$targetVerticalScroll = 100
[string]$uri = "https://www.test."

$objIE.Navigate($uri)
[sfw]::SetForegroundWindow($HWND) | Out-Null

#Omit the next line if running as administrator. Else, see below ment for a link
$objIE = ConnectIExplorer -HWND $HWND

$objIE.Document.parentWindow.scroll($targetHorizontalScroll,$targetVerticalScroll)

This is a much more controlled and repeatable method than sendkeys. When using SendKeys the amount of pixels which you scroll is dependent on the window size, where in this code you scroll an absolute number of pixels, regardless of window size.

My code also uses the ConnectIExplorer function from the answer here:

PowerShell IE9 ComObject has all null properties after navigating to webpage

Which fixes an issue with Protected Mode interfering with scripting of IE Com objects when the script is to be run without elevated permissions.

For convenience, that function by user @bnu is also reproduced here:

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 - Object InternetExplorer. Aborting." -ForegroundColor Red
        $objNewIE = $null
      }     
    } finally { 
      $ErrorActionPreference = $EA
      $objShellApp = $null
    }
    return $objNewIE
  } 

Also it is worth noting that @ruffin's answer contains an error. As written it will type "DOWN 10" instead of sending the down arrow ten times (and accidentally scroll down slightly because it includes a spacebar press). This can be fixed with a second set of curly brackets like so:

[System.Windows.Forms.SendKeys]::SendWait({{DOWN 10}})

I think you're looking for really quick and dirty. If that's true, and you don't mind ugly, try using SendKeys.

$ie = new-object -object InternetExplorer.Application -property `
    @{navigate2="http://testmy/SmarTest/binedAuto"; 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"

Keep messing around with the number of down arrows you send until it's right -- so edit {DOWN 10}.

NOTE: Chirishman says that you need to have two squiggle brackets around DOWN 10 -- {{DOWN 10}}. The version above almost certainly worked verbatim on my box at the time of writing, but ymmv.

Scared you're going to have enough timing issues that you eventually go back and use another tool, however. How many of these do you have to take?

Note that I did change the URL to espn. while testing. Not sure what's going on at yours -- a speed test? Seemed to load about three different pages.

本文标签: javascriptPowershell Scroll down a webpagePowershell ScreenshotStack Overflow