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”