Saving page in file

Good morning,
I’m developing a project in Powershell. My code is below. Basically it should log into gmail site and save content to file. But I am not getting save the page of my emails on file. I’ve done ​​several attempts and I am not getting save the page results in file. I am sending the code to log into gmail … please if you can help save the result in a txt file I thank you very much. Follows the code for your review:

$ie = new-object -com “InternetExplorer.Application”
$ie.navigate(“about:blank”)
$doc = $ie.Document
$ie.visible = $true

----------------------------------------------------------------------------------------------------------------------------------------------

Function NavigateTo([string] $url, [int] $delayTime = 2000)
{
Write-Verbose “Navigating to $url”;
$ie.Navigate($url)
WaitForPage $delayTime
}

----------------------------------------------------------------------------------------------------------------------------------------------

Function WaitForPage([int] $delayTime = 2000)
{
$loaded = $false
while ($loaded -eq $false) {
[System.Threading.Thread]::Sleep($delayTime)
#If the browser is not busy, the page is loaded
if (-not $ie.Busy)
{
$loaded = $true
}
}

$doc = $ie.Document
}

----------------------------------------------------------------------------------------------------------------------------------------------

Function SetElementValueByID($id, $value) {
if ($doc -eq $null) {
Write-Error “Document is null”;
break
}

$element = [System.__ComObject].InvokeMember(“getElementById”,[System.Reflection.BindingFlags]::InvokeMethod, $null, $doc, $id)
if ($element -ne $null) {
$element.Value = $value
}
else {
Write-Warning “Couldn’t find any element with id “”$id”"";
}
}

----------------------------------------------------------------------------------------------------------------------------------------------

Function ClickElementById($id)
{
$element = [System.__ComObject].InvokeMember(“getElementById”,[System.Reflection.BindingFlags]::InvokeMethod, $null, $doc, $id)
if ($element -ne $null) {
$element.Click()
WaitForPage
}
else {
Write-Error “Couldn’t find element with id “”$id”""
break
}
}

----------------------------------------------------------------------------------------------------------------------------------------------

Function ClickElementByTagName($tagName, [int] $position = 0)
{
if ($doc -eq $null) {
Write-Error “Document is null”
break
}
$elements = @([System.__ComObject].InvokeMember(“getElementsByClassName”,[System.Reflection.BindingFlags]::InvokeMethod, $null, $doc, $tagname))
if ($elements.Count -ne 0) {
$elements[$position].Click()
WaitForPage
}
else {
Write-Error “Couldn’t find element “”$tagName”" at position “”$position""";
break
}
}

NavigateTo “http://www.gmail.com”;
SetElementValueByID “Email” “someone@gmail.com
SetElementValueByID “Passwd” “mypassword”
ClickElementById “signIn”

I don’t see anything in your code that attempts to save a page to disk. I haven’t tested it, but it looks like you just fill in the username / password boxes and click “Sign in”, then do nothing else.

This is my dilemma. I tried various codes to try to save the page to disk, but none worked. I was wondering how do I go from here to save the contents of the page to disk.

I tried at the end:

Start-Sleep -s 5
$ie.document.body.outerHTML | Out-File -FilePath c:\MyPage.html

but not work.

I’ve tried this way, but it generates an empty file at any site.
I also tried the following:

$web = New-Object Net.WebClient
$web.DownloadString($ie.LocationURL)

But it does not work.
The most we can is save the page, but no information after login (the contents of the email).

The exact contents of the logged window I can not capture.

I know it’s kind of boring, but I burned both their heads trying… I dont see no more solution.

I’m no longer able to think of anything more to solve it.

Are you aware of the Invoke-WebRequest cmdlet? and Invoke Invoke-RestMethod?

http://technet.microsoft.com/library/hh849901.aspx

http://technet.microsoft.com/library/hh849971.aspx

Google may have a restful API to do what you’re doing. You’re basically trying to screen scrape using the status of Internet Explorer as it loads a page.

That being said anything you collect in your variables can be written to file quite easily.

$variablename | Out-File -FilePath d:\temp\filename.txt -Append

Unless you mean that you are trying to write a file to the google drive that would need another API and a lot more coding.

If you do mean Google drive they have an application that you can install that runs in the system tray that allows you to treat it as a local drive. I have never used it because I prefer one drive by Microsoft.

https://tools.google.com/dlpage/drive

https://developers.google.com/apis-explorer/#p/drive/v2/ << here is their cryptic API for google drive

Thank you for your help,
but it would be to save it to disk.

The problem is that the code: $ie.document.body.outerHTML
returns nothing for me.

So is the purpose of this script to auto log you into your gmail page?

have you heard of secure utilities that do this exact thing for you such as https://lastpass.com/

I still think the screen scraping method and looking for title bar changes in the internet Explorer browser is an old school way of doing things. It’s a fun little project if that is all you’re trying to do with it but your password to your gmail account will live in the script in clear text. It’s a fun learning exercise but if the end goal is to use it daily I would recommend lastpass or something like it.

To answer your question I guess you could do this. . .

ie.Document.body.outerHTML | Out-File d:\temp\gmail.html

But the results did not look good when I tried it. This would in theory I guess save a snapshot of your inbox as a static HTML page on your disk but to what end? I’m not sure what it accomplishes. If I understood your goals I could try and help better…sorry

I would spend your time trying to figure out their restful API if automating email actions is what you’re trying to do.

-VERN