Private/FacebookAccountLogin.ps1
function FacebookAccountLogin { [CmdletBinding()] param( [parameter(Mandatory=$True)] $SeleniumDriver, [parameter(Mandatory=$True)] [pscredential]$PSCreds ) # Determine if the Authentication window is in the Selenium Parent Window or in a Child PopUp Window if (@($SeleniumDriver.WindowHandles).Count -gt 1) { try { # The Authentication propmpt is in a Child Window that just opened $SeleniumDriver.SwitchTo().Window($SeleniumDriver.WindowHandles[-1]) $ParentWindowHandle = $SeleniumDriver.WindowHandles[0] } catch { Write-Error $_ return } } # Next, click the Facebook Login screen Email field try { $EmailField = Get-SeElement -By XPath -Selection '//*[@id="email"]' -Target $SeleniumDriver if (!$EmailField) { throw "Cannot find Facebook Email field! Halting!" } Send-SeClick -Element $EmailField -Driver $SeleniumDriver } catch { Write-Error $_ return } # Enter the user's email address #[OpenQA.Selenium.Interactions.Actions]::new($SeleniumDriver).SendKeys('ldimaggiott@gmail.com').Perform() try { Send-SeKeys -Element $EmailField -Keys $PSCreds.UserName -ErrorAction Stop } catch { Write-Error $_ return } # Get the Facebook Password field try { $PwdField = Get-SeElement -By XPath -Selection '//*[@id="pass"]' -Target $SeleniumDriver if (!$PwdField) { throw "Cannot find Google Email 'Next' button! Halting!" } Send-SeClick -Element $PwdField -Driver $SeleniumDriver } catch { Write-Error $_ return } # Enter the user's password try { $PwdPT = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($PSCreds.Password)) Send-SeKeys -Element $PwdField -Keys $PwdPT -ErrorAction Stop } catch { Write-Error $_ return } # Click the "Login" button to complete Login try { $LoginButton = Get-SeElement -By XPath -Selection '//*[@id="loginbutton"]' -Target $SeleniumDriver if (!$LoginButton) { throw "Cannot find Facebook 'Login' button! Halting!" } Send-SeClick -Element $LoginButton -Driver $SeleniumDriver } catch { Write-Error $_ return } if ($ParentWindowHandle) { $SeleniumDriver.SwitchTo().Window($ParentWindowHandle) } } |