![]()
Playwright Guide: How To Find Elements by CSS Selector
Whenever we automate actions with a web browser, we need to locate and select elements. Without the ability to select an element, we can't click, we can't fill form data, we can't even scroll to the element because our Playwright doesn't know where it is.
If Playwright can't find page elements, it can't really do much other than open new pages and close the browser!
In this guide, we're going to learn how to find elements using their CSS selector.
- TLDR: How To Find Elements by CSS Selector
- Understanding CSS Selectors
- Basic CSS Selectors
- Advanced CSS Selectors
- Debugging and Troubleshooting
- Conclusion
- More Web Scraping Guides
Need help scraping the web?
Then check out ScrapeOps, the complete toolkit for web scraping.
TLDR: How To Find Elements by CSS Selector
To find elements by CSS selector in Node.js using Playwright, follow the script below:
const playwright = require("playwright");
async function main() {
//open a browser
const browser = await playwright.chromium.launch();
//open a new page
const page = await browser.newPage();
//navigate to the site
await page.goto("https://quotes.toscrape.com");
//find the first h1 element
const h1 = page.locator("h1").first();
const h1Text = await h1.textContent();
console.log(`H1 element: ${h1Text}`);
//find all elements with the class "quote"
const quotes = page.locator(".quote");
//get the count of our quotes list
const quoteCount = await quotes.count();
//iterate through quotes
for (let i=0; i<quoteCount; i++) {
//get the text of the quote
text = await quotes.nth(i).textContent();
//log it to the console
console.log(`Quote: ${text}`);
}
//close the browser
await browser.close();
}
main()
- First, we initiate a Chromium browser instance using Playwright's
launch()function. - Within the browser, we create a new page via browser.newPage() and navigate to the URL Quotes to Scrape .
- We locate the first
h1element on the page usingpage.locator("h1").first().- Find By Tag Name: Pass the tag name into
locator()...page.locator("h1") - Find By Class Name: Pass
.name-of-classintolocator()...page.locator(".quote") - Find By ID: Pass
#idintolocator()...page.locator("#username")
- Find By Tag Name: Pass the tag name into
- After, we retrieve the text content of the found
h1element usingh1.textContent()and print the text content of theh1element to the console. - We locate all elements with the class "quote" on the page using
page.locator(".quote"). - We print the text content of each quote to the console and finally, we close the browser instance to release system resources using
browser.close().
Understanding CSS Selectors
CSS selectors are used in order to style a webpage. Any page with more than just white background and black text uses them. Often when styling a webpage, a developer will use a class in order to style things.
CSS selectors extend much further than just simple classes, but let's take a look at a CSS class just to show how CSS works on a webpage.
For starters, we can create a new HTML file.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Our Demo Page</title>
<link rel="stylesheet" type="text/css" href="demo.css">
</head>
<body>
<h1>Hello I am an HMTL File</h1>
<p>These are some smaller words.</p>
</body>
</html>
If you open this file in your browser, it will look like the image below... Pretty boring right?

Next, we're going to create a class. Since our HTML file already links to "demo.css", let's create a new CSS file, "demo.css". Here is our new class, it's not much, just enough to show you what CSS does.
.our-new-class {
background-color: black;
color: white;
}
Next, we update our file to use our new class. We do one thing: add class="our-new-class" to the body element.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Our Demo Page</title>
<link rel="stylesheet" type="text/css" href="demo.css">
</head>
<body class="our-new-class">
<h1>Hello I am an HMTL File</h1>
<p>These are some smaller words.</p>
</body>
</html>
Here is a screenshot of the page after giving it new style.

However, there is for more to CSS than just classes. Take a look at the table below.
When using CSS selectors on a page, we can select them by:
- tag,
- id,
- class,
- attribute,
- descendant,
- child,
- adjacent sibling,
- general sibling,
- pseudo-element and
- pseudo-class.
| Selector Type | CSS Syntax | Description |
|---|---|---|
| Tag Name | tag | Selects elements by their tag. |
| ID | #id | Selects an element by its ID. |
| Class | .class | Selects elements by their class. |
| Attribute | [attribute=value] | Selects elements with a specific attribute. |
| Descendant | ancestor descendant | Selects an element within another element. |
| Child | parent > child | Selects direct children of an element. |
| Adjacent Sibling | previous + next | Selects an element directly after another. |
| General Sibling | sibling ~ sibling | Selects all siblings of a specified element. |
| Pseudo-class | element:pseudo-class | Selects elements in a certain state. |
Basic CSS Selectors in Playwright
Let's dive into some basic CSS selectors and their usage in Playwright: