title: Selenium Navigating Through Outlook Mail Login (Headless Driver)
body: <p>I am using Selenium and Python 3.9 to navigate through the Outlook mail login. The goal is to reach the main inbox page after logging in successfully. I am using a chrome driver and am experiencing errors when running the script below to navigate through Outlook's login. When I run a headless driver or simply minimize the window without going headless, the <strong>login page seems to freeze before clicking the 'Next' button on the password page</strong> no matter how long I set the timeout time. Is there a solution to this?</p>
  <p>The code below can be tested after you specify your Outlook login username, password, and the path to where the Chrome driver was downloaded:</p>
  <pre><code>from bs4 import BeautifulSoup
  from selenium import webdriver
  from selenium.webdriver.common.by import By
  from selenium.webdriver.support.ui import WebDriverWait
  from selenium.webdriver.chrome.options import Options
  from selenium.webdriver.support import expected_conditions as EC
  from selenium.common.exceptions import NoSuchElementException
  from selenium.common.exceptions import StaleElementReferenceException
  import time
  
  # -- Login Credentials
  o_username = ' OUTLOOK LOGIN USERNAME '
  o_password = ' OUTLOOK LOGIN USERNAME '
  
  # -- Headless Driver (Ideal)
  #o_options = Options()
  #o_options.headless = True
  #o_driver = webdriver.Chrome(options=o_options, executable_path=r' PATH TO DRIVER DOWNLOAD ')
  
  # -- Minimized Window (Not ideal, but accepted)
  o_driver = webdriver.Chrome(r' PATH TO DRIVER DOWNLOAD ')
  o_driver.minimize_window()
  
  o_driver.get('https://outlook.office.com/mail/inbox')
  
  # -- Fill username and click next
  WebDriverWait(o_driver, 10).until(EC.presence_of_element_located((By.ID, 'i0116'))).send_keys(o_username)
  WebDriverWait(o_driver, 10,(NoSuchElementException,StaleElementReferenceException,)).until(EC.presence_of_element_located((By.ID, 'idSIButton9'))).click()
  
  time.sleep(3)
  
  # -- Fill password and click next
  WebDriverWait(o_driver, 10).until(EC.presence_of_element_located((By.ID, 'i0118'))).send_keys(o_password)
  WebDriverWait(o_driver, 10, (NoSuchElementException,StaleElementReferenceException,)).until(EC.presence_of_element_located((By.ID, 'idSIButton9'))).click()
  
  try:
      # -- Detect HTML element only on main inbox page
      WebDriverWait(o_driver, 2).until(EC.presence_of_element_located((By.ID, 'Pivot29-Tab0')))
  except:
      # -- Handle the 'Stay Logged In?' page and click 'Yes'
      WebDriverWait(o_driver, 5).until(EC.presence_of_element_located((By.ID, 'idSIButton9'))).click()
      time.sleep(3)
  
      # -- Detect the element on main inbox page
      # -- ERROR OCCURS HERE BY TIMEOUT BECAUSE PAGE IS STILL STUCK ON PASSWORD FILL-IN PAGE
      WebDriverWait(o_driver, 5).until(EC.presence_of_element_located((By.ID, 'Pivot29-Tab0')))
  
  # -- Main Inbox Loaded
  </code></pre>