Thursday, 10 March 2016

Powershell to log into Gmail

I'm automating some tasks, one of which involves invoking a browser and signing into gmail. I wrestled with problems for an hour or so till I realised my tag name for the final submit was actually "input".  I've pasted the currently working script below in case it can be of use.


# Create New IE Object in visible browser window
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true

# Open URL
$ie.Navigate("mail.google.com") 

# Let page load
while ($ie.Busy -eq $true){Start-Sleep -seconds 4;}

# Enter username / email address
$usernamefield = $ie.Document.getElementByID('Email')
$usernamefield.value = 'your-email-here@gmail.com'

# CLICK "Next" button
$next=$ie.Document.getElementByID('next')
$next.click()

# Enter password
$passwordfield = $ie.Document.getElementByID('Passwd-hidden')
$passwordfield.value = 'your-password-here'

# Click the "Sign In" Button
# ELEMENT: 
# THEREFORE the tag name of your button is "input" and the type is "submit"
$signIn = $ie.Document.getElementsByTagName("input") | where-object {$_.type -eq "submit"}
$signIn.click();

No comments:

Post a Comment