Playwright Python Get Width and Height of Element

Profile picture for user arilio666

The element.bounding_box() method in Playwright with Python can be used to get the width and height of an element.

Playwright Python Get Width and Height of Element

Let us get the height and width of this login button from the login page of programsbuzz.

Example:

import asyncio
from playwright.sync_api import sync_playwright, expect
with sync_playwright() as p:
  browser = p.chromium.launch(headless=False)
  page = browser.new_page()
  page.goto('https://www.programsbuzz.com/user/login')
  loginButton =  page.locator('//*[@id="edit-submit"]')
  box =  loginButton.bounding_box()
  print(box['height']);
  print(box['width']);
  • We run a Chromium browser and navigate to programsbuzz's login page using Playwright's sync_playwright. 
  • Provide the XPath or CSS selector for the element you wish to measure.
  • We use element.bounding_box() to get the bounding box information after we have the element. 
  • The bounding box dictionary is then used to obtain the width and height.

Playwright Python Get Width and Height of Element