In this section, we will explore how to effectively use selectors in Playwright to interact with web elements. Selectors are a fundamental part of writing automated tests, as they allow you to locate and manipulate elements on a webpage. Understanding how to use selectors efficiently will enable you to write robust and reliable tests.

Key Concepts

  1. Selectors Overview:

    • Selectors are strings used to identify elements on a webpage.
    • Playwright supports various types of selectors, including CSS, XPath, and text selectors.
  2. Types of Selectors:

    • CSS Selectors: Use CSS syntax to select elements.
    • Text Selectors: Select elements based on their text content.
    • XPath Selectors: Use XPath expressions to locate elements.
    • Role Selectors: Select elements based on their ARIA roles.
  3. Selector Strategies:

    • Use the most specific selector possible to avoid ambiguity.
    • Prefer CSS selectors for their simplicity and performance.
    • Use text selectors for elements with unique text content.

Practical Examples

Example 1: Using CSS Selectors

import { chromium } from 'playwright';

(async () => {
  const browser = await chromium.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');

  // Select an element using a CSS selector
  const element = await page.$('h1');
  const text = await element?.textContent();
  console.log(text); // Outputs the text content of the <h1> element

  await browser.close();
})();

Explanation:

  • We launch a Chromium browser and navigate to a webpage.
  • We use a CSS selector (h1) to select the first <h1> element on the page.
  • We retrieve and log the text content of the selected element.

Example 2: Using Text Selectors

import { chromium } from 'playwright';

(async () => {
  const browser = await chromium.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');

  // Select an element using a text selector
  const element = await page.$('text="Example Domain"');
  const text = await element?.textContent();
  console.log(text); // Outputs "Example Domain"

  await browser.close();
})();

Explanation:

  • We use a text selector (text="Example Domain") to select an element with the exact text "Example Domain".
  • This is useful for selecting elements with unique text content.

Example 3: Using XPath Selectors

import { chromium } from 'playwright';

(async () => {
  const browser = await chromium.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');

  // Select an element using an XPath selector
  const element = await page.$('//h1');
  const text = await element?.textContent();
  console.log(text); // Outputs the text content of the <h1> element

  await browser.close();
})();

Explanation:

  • We use an XPath selector (//h1) to select the first <h1> element on the page.
  • XPath selectors are powerful but can be more complex than CSS selectors.

Exercises

Exercise 1: Select an Element by Class Name

Task: Write a Playwright script to select an element with the class name example-class and log its text content.

Solution:

import { chromium } from 'playwright';

(async () => {
  const browser = await chromium.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');

  // Select an element by class name
  const element = await page.$('.example-class');
  const text = await element?.textContent();
  console.log(text);

  await browser.close();
})();

Feedback: Ensure the class name is unique to avoid selecting multiple elements. If multiple elements have the same class, consider using a more specific selector.

Exercise 2: Use a Role Selector

Task: Write a Playwright script to select a button element with the role button and log its text content.

Solution:

import { chromium } from 'playwright';

(async () => {
  const browser = await chromium.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');

  // Select a button element by role
  const element = await page.$('role=button');
  const text = await element?.textContent();
  console.log(text);

  await browser.close();
})();

Feedback: Role selectors are useful for accessibility testing. Ensure the element has the correct ARIA role attribute.

Conclusion

In this section, we covered the basics of working with selectors in Playwright. We explored different types of selectors and how to use them effectively in your tests. By mastering selectors, you can write more precise and reliable automated tests. In the next section, we will delve into handling multiple pages and frames, which will further enhance your testing capabilities.

© Copyright 2024. All rights reserved