Microsoft Live login

On this page

Playwright and Puppeteer also allow us to automate logging in to a Microsoft Live account.

Steps

  1. We start at https://login.live.com
  2. We provide the username and password, injected by using environment variables
  3. We are redirected to the main account page

const { chromium } = require('playwright')

;(async () => {
  const browser = await chromium.launch({ headless: false })
  const page = await browser.newPage()

  await page.setViewport({ width: 1280, height: 800 })
  await page.goto('https://login.live.com/')

  await page.fill('[name="loginfmt"]', process.env.MSLIVE_USER)
  await page.click('[type="submit"]')

  await page.fill('input[type="password"]', process.env.MSLIVE_PWD)
  await page.keyboard.press('Enter')

  await browser.close()
})()



const puppeteer = require('puppeteer')

;(async () => {
  const browser = await puppeteer.launch({ headless: false })
  const page = await browser.newPage()

  await page.setViewport({ width: 1280, height: 800 })
  await page.goto('https://login.live.com/')

  const navigationPromise = page.waitForNavigation()

  await page.waitForSelector('[name="loginfmt"]')
  await page.type('[name="loginfmt"]', process.env.MSLIVE_USER)
  await page.click('[type="submit"]')

  await navigationPromise
  await page.waitForSelector('input[type="password"]', { visible: true })
  await page.type('input[type="password"]', process.env.MSLIVE_PWD)
  await page.keyboard.press('Enter')

  await navigationPromise
  await browser.close()
})()


Run this example as follows. Replace the username and password placeholder with your own credentials.

MSLIVE_USER=username MSLIVE_PWD=password node mslive-login.js
SET MSLIVE_USER=username
SET MSLIVE_PWD=password
node mslive-login.js

This example does not work when you have 2-factor authentication enabled, and you might trigger a recaptcha check.

Takeaways

  1. Use environment variables to inject secrets.
  2. Wait for the navigation as your are redirected to Microsoft.
  3. Wait for the navigation as you are redirected back to the start site.

Last updated on March 25, 2024. You can contribute to this documentation by editing this page on Github