Frontend Development 3 min read

Using Playwright in Python to Select, Click, Fill, and Retrieve Attributes from Web Page Elements

This guide demonstrates how to use Python's Playwright library to select single or multiple elements via CSS selectors, click buttons, fill form fields, and retrieve element attributes, providing example code that opens a browser, navigates to a page, and performs these interactions.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Using Playwright in Python to Select, Click, Fill, and Retrieve Attributes from Web Page Elements

In Python's Playwright library you can select and manipulate page elements, including buttons, form fields, and retrieve their attributes.

1. Selecting elements: Use page.query_selector() to select a single element and page.query_selector_all() to select multiple elements, passing CSS selectors as arguments.

# Select a single element element = page.query_selector("#myButton") # Select multiple elements elements = page.query_selector_all(".myClass")

2. Operating buttons: After selecting a button element, call click() to simulate a click.

button = page.query_selector("#myButton") button.click()

3. Operating forms: Use fill() on a selected input element to set its value.

input_field = page.query_selector("#myInput") input_field.fill("Hello, World!")

4. Getting attributes: Call get_attribute() on a selected element to obtain the value of a specific attribute.

element = page.query_selector("#myElement") attribute_value = element.get_attribute("attribute_name") print(attribute_value)

Example code: The following script launches a Chromium browser, navigates to a page, clicks a button, fills a form field, retrieves an attribute, prints it, and then closes the browser.

from playwright.sync_api import sync_playwright def interact_with_elements(): with sync_playwright() as playwright: browser = playwright.chromium.launch() page = browser.new_page() # Navigate to the page containing button, form, and attribute page.goto("https://example.com") # Select and click the button button = page.query_selector("#myButton") button.click() # Fill the form field input_field = page.query_selector("#myInput") input_field.fill("Hello, World!") # Get element attribute element = page.query_selector("#myElement") attribute_value = element.get_attribute("attribute_name") print(attribute_value) browser.close() interact_with_elements()

The example demonstrates how Playwright can be used in Python to programmatically interact with web page elements for testing or automation purposes.

PythonTestingPlaywrightWeb AutomationWeb Interaction
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.