table of contents Table of contents

How to Automate Microsoft Live Login with Playwright

On this page

Playwright allows 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()
})()

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 November 13, 2024. You can contribute to this documentation by editing this page on Github