My Internet Explorer object $oIE
, won't let me access the page's body property if it concerns a PDF file (as opposed to a HTML page). If I try to access that property, my code breaks. Here is how I call it:
_IEAction($oIE, 'saveas')
But it errors:
"C:\Program Files (x86)\AutoIt3\Include\IE.au3" (1959) : ==> The requested action with this object has failed.:
$oObject.document.execCommand("SaveAs")
$oObject.document^ ERROR
I need to iterate over a few pages of PDF files and save them to disk. This error is thrown only when the page is a PDF document; a normal HTML page works fine. How to check if the document body property exists? If it doesn't, it means the page is a PDF (I need to save it).
You will want to use the InetGet function to save the PDF's and check the URL to see if it is a PDF file.
Here is a simple example using InetGet.
InetGet("http://careers.stackoverflow.com/stack_overflow_careers.pdf", @ScriptDir & "\stack_overflow_careers.pdf")
Here is an example of finding all PDF URL's on a page and downloading those PDF's.
#include <IE.au3>
#include <Array.au3>
DownloadAllPDFs("http://careers.stackoverflow.com/resources/great-job-listing")
Func DownloadAllPDFs($URL)
Local $oIE = _IECreate($URL)
Local $oLinks = _IELinkGetCollection($oIE)
Dim $aPDFLinks[1]
For $oLink In $oLinks
If StringInStr($oLink.href, ".pdf") Then
_ArrayAdd($aPDFLinks, $oLink.href)
EndIf
Next
Local $iArraySize = UBound($aPDFLinks) - 1
ConsoleWrite("Number of PDF Files found: " & $iArraySize)
;_ArrayDisplay($aPDFLinks)
If $iArraySize > 0 Then
For $i = 1 To $iArraySize
InetGet($aPDFLinks[$i], @ScriptDir & "\" & $i & ".pdf")
Next
EndIf
EndFunc ;==>DownloadAllPDFs
Here is an example of navigating to a URL, and downloading the file if it is a PDF.
#include <IE.au3>
NavigateAndDownload()
Func NavigateAndDownload()
Local $oIE = _IECreate()
_IENavigate($oIE, "http://careers.stackoverflow.com/stack_overflow_careers.pdf", 0)
Sleep(5000)
$sURL = _IEPropertyGet($oIE, "locationurl")
If StringInStr($sURL, ".pdf") Then InetGet($sURL, @ScriptDir & "\test.pdf")
EndFunc