0% found this document useful (0 votes)
17 views6 pages

Playwright Python

The document provides a guide for installing Python and Visual Studio Code, along with the necessary extensions for using UV. It details commands for managing packages and introduces Playwright as a testing tool, comparing it to Selenium and outlining its features, locators, and actions. Additionally, it includes examples of locator types and actions that can be performed using Playwright.

Uploaded by

pataneshashi09
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views6 pages

Playwright Python

The document provides a guide for installing Python and Visual Studio Code, along with the necessary extensions for using UV. It details commands for managing packages and introduces Playwright as a testing tool, comparing it to Selenium and outlining its features, locators, and actions. Additionally, it includes examples of locator types and actions that can be performed using Playwright.

Uploaded by

pataneshashi09
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Install Python [Link]

org/downloads/
Install VScode [Link]

Install Python Extensions by MS


Install UV [Link]

uv init :- to setup the uv inside the project folder


uv add <package_name> :- to add the package
uv remove <package_name> :- to remove the package
uv tree :- to see the dependent package
uv sync :- to setup venv, activate venv and install packages
Playwright doc - [Link]

Feature Selenium Playwright

Browser Uses system browsers Downloads its own browsers


dependency

Driver Yes (ChromeDriver, No driver needed


required GeckoDriver, etc.)

Version Depends on your browser Playwright manages versions


control version automatically

Setup Browser + driver needed Just playwright install

Supported Chrome, Edge, Firefox, Chromium, Firefox, WebKit


browsers Safari
Playwright locators - [Link]

Locator Type Syntax Example Description / When to Use

CSS Selector [Link]("[Link]") Default & fastest locator. Uses standard CSS syntax like class, id,
tag, etc.

Text Locator page.get_by_text("Login") Finds an element by its visible text on the page. Ideal for buttons or
links.

Role Locator page.get_by_role("button", Based on accessibility role (button, textbox, etc.). Very reliable and
name="Submit") readable.

Label Locator page.get_by_label("Username") Targets input elements tied to a <label>. Great for form fields.

Placeholder page.get_by_placeholder("Enter email") Finds elements by placeholder text, usually used in login/signup
Locator forms.

Alt Text Locator page.get_by_alt_text("Company Logo") Locates images or elements using the alt attribute (helpful for
accessibility).

Title Locator page.get_by_title("User Settings") Locates elements that have a title attribute tooltip.
Playwright locators - [Link]

Locator Type Syntax Example Description / When to Use

Test ID Locator page.get_by_test_id("submit-button") Uses data-testid or similar attributes. Perfect for


stable automation in CI/CD.

XPath Selector [Link]("//div[@class='header']") Uses XPath syntax to traverse DOM. Slower but
powerful for complex HTML structures.

Nth Locator [Link]("button").nth(1) Selects a specific element by index if multiple are


matched.

Chained Locator [Link](".form").locator("button") Finds child elements within a parent (scoped


search).

Filter Locator [Link]("div").filter(has_text="Settings") Filters matching elements based on text or nested


content.

Frame Locator page.frame_locator("#iframe-id").locator("button") Used for finding elements inside an iframe.


Playwright Actions - [Link]
Action Command Example Description / Usage

Click Element [Link]("[Link]").click() Clicks an element once it’s visible & enabled.

Type Text [Link]("#username").type("anshul") Types characters one by one (simulates real typing).

Fill Textbox [Link]("#username").fill("anshul") Quickly fills or replaces text (faster than .type()).

Check Checkbox / Radio [Link]("#accept").check() Checks a checkbox or radio button safely.

Uncheck Checkbox [Link]("#accept").uncheck() Unchecks a checkbox if already checked.

Select Dropdown Option [Link]("#country").select_option("IN") Selects option from a <select> dropdown.

Hover Element [Link](".menu-item").hover() Moves mouse over an element (for hover menus).

Press Key [Link]("#input").press("Enter") Simulates keyboard key press (Enter, Tab etc.).

Upload File [Link]("#upload").set_input_files("[Link]") Uploads a local file to a file input.

Clear Input Field [Link]("#username").clear() Clears existing text from a textbox.


Playwright Actions - [Link]
Action Command Example Description / Usage

Wait for Element page.wait_for_selector(".success-msg") Waits until an element appears or is visible.

Scroll Into View [Link]("#footer").scroll_into_view_if_needed Scrolls until element is visible on screen.


()

Get Text Content [Link](".msg").text_content() Returns the inner text of the element.

Get Attribute Value [Link]("img").get_attribute("src") Fetches value of a specific HTML attribute.

Is Visible [Link](".popup").is_visible() Returns True if element is displayed.

Is Enabled [Link](".btn").is_enabled() Checks if button/input is enabled for


interaction.

Screenshot Page [Link](path="[Link]") Takes screenshot of entire page.

Screenshot [Link]("#logo").screenshot(path="[Link]") Captures only the selected element.


Element

You might also like