1

I want to download images from a website to check against other dynamic information on the site which is loaded in via AJAX. Since the site uses AJAX to load in the other information I can't use an invoke-webrequest because the ajax information doesn't load or at least I can't figure out how to trigger it. For that reason I decided to use a com object of Application.internetexplorer.

This works to traverse and trigger the javascript but I can't figure out how to save the picture that is being displayed in the IE window. I've tried to download the image via the src path but, as stated previously, the image changes from the original state slightly on every request. There is no 'save' method that I can see from $ie.document.images or any other path. I've tried to out-file to a .png as innerHTML or outerHTML etc. as a last ditch effort but that all fails as well.

I can see the picture when I make the com object visible. There must be some way to grab that image which is already displaying on my computer. Does anyone know how I could accomplish this?

Not sure it Helps but here is my code so far:

    $url = "https://somesite.com"
    $ie = New-Object -com internetexplorer.application
    $ie.navigate($url)
    while($ie.ReadyState -ne 4) {start-sleep -seconds 1} 
    $ie.visible = $true    
    $doc = $ie.Document
    $firstImage= $doc.IHTMLDocument3_getElementById('images01')
    $secondImage = $doc.IHTMLDocument3_getElementById('images02')
    $thirdImage = $doc.IHTMLDocument3_getElementById('images03')

    #how can I save firstImage as .png?

1 Answer 1

1

Seems like you are most of the way there. Can't you now just add

Invoke-WebRequest -Uri $firstImage.href -OutFile c:\temp\...

There may be an alternative by creating an IHTMLControlRange, adding the DOM object, then using method execCommand("saveas") on the range, but that will always display a SaveAs... dialog for security reasons. There is a rather old web post titled Save the Last GIF for Me that might help you understand how execCommand is upposed to work, but so far I've not gotten it to work from PowerShell, so Invoke-WebRequest approach seems a safer bet to me, and won't necessarily put up a dialog box you may not want.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the help. Unfortunately I can't use another web request such as Invoke-WebRequest because the picture changes slightly on each request. Therefor the picture from the IE com object and the picture from the Invoke-WebRequest would be different.
I'm trying to use the IHTMLControlRange but Can't figure it out. I tried adding a range, passing in $firstImage, and then executing execCommand but nothing gets copied.
Sounds like what I ran into too. Its a guess but I think it might be because the DOM objects are out-of-process. Perhaps instead of manipulating the DOM in PowerShell, you could write a little JavaScript to create the control range and run execCommand, then inject it into the browser. Also note the command "copy" might avoid the GUI by putting the image on the clipboard.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.