After we got Playwright up and running in the last post, we can now take a closer look on how we can automate a web browser with Playwright.
Navigate to the web page
Before we can start looking for elements, we need to open the web page we are interested in. We can call the GotoAsync() method on our Page instance:
1 |
await Page.GotoAsync("https://improveandrepeat.com/"); |
The Page object comes from the class PageTest which we inherited from in our test class. If you run Playwright outside of a test class, you can use this code to get the Page object:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using Microsoft.Playwright; using System; using System.Threading.Tasks; class Program { public static async Task Main() { using var playwright = await Playwright.CreateAsync(); await using var browser = await playwright.Chromium.LaunchAsync( new BrowserTypeLaunchOptions { Headless = false, }); var context = await browser.NewContextAsync(); var Page = await context.NewPageAsync(); await Page.GotoAsync("https://improveandrepeat.com/"); } } |
Get the title of a page
We can get the title of the current page with the TitleAsync() method:
1 |
var title = await Page.TitleAsync(); |
Selectors & locators
Playwright uses selectors and locators to interact with elements on a web page. The selectors are the strings we use to define the elements we want, while the locators are the objects on which we call the methods to perform the desired actions (e.g., Locator.ClickAsync() to click a button).
Playwright offers various ways to define your selectors. I will cover only the most basic selectors to get you started. When you need more, check the official documentation to find a better match. And while you are there, read the section on best practices – following this advice will make your tests more robust.
Text selectors help us to find elements using the text they display. If we have an element with the text “Log in”, we can create a selector like this:
1 |
var logIn = await page.Locator("text=Log in"); |
CSS selectors operate on the shadow DOM. We can use selectors as we know from CSS and use things like the nth-matcher to find the last button on a page:
1 |
var button = await page.Locator("button >> nth=-1"). |
There is no cut-and-dry separation between these selectors. We can combine a text selector with a CSS selector to get exactly the element we are looking for:
1 2 3 |
var clickMe = await page.Locator("button", new() { HasText = "Click me" }); var menuHome = await page.Locator("#nav-bar :text('Home')"); |
If we want to search an element by its ID, we can add a # as a prefix or use the id= shorthand:
1 2 3 |
var lang = await Page.Locator("id=de").InnerTextAsync(); var lang2 = await Page.Locator("#de").InnerTextAsync(); |
These examples only scratch the surface of selectors. The more you work with Playwright, the simpler it gets to use the combination of text and CSS selectors. Start with the examples from above and then add more if you need a more precise selector.
Fill input forms
If we get an element that accepts a text input, we can write to it with the FillAsync() method:
1 |
await Page.Locator("id=name").FillAsync("Johnny"); |
Click buttons
We can click buttons with the ClickAsync() method:
1 |
await Page.Locator("button >> nth=-1").ClickAsync(); |
Read values from elements
The methods InnerTextAsync() and TextContentAsync() allow us to access the text of an element through the locator:
1 2 3 |
var description = await Page.Locator(".news >> nth=0").TextContentAsync(); var description2 = await Page.Locator(".news >> nth=0").InnerTextAsync(); |
If the element contains other elements with text and we want everything in one go, we can use the method AllInnerTextsAsync() to get the content as a list of strings:
1 2 3 4 5 |
var allText = await Page.Locator("article").AllInnerTextsAsync(); foreach (var text in allText) { Console.WriteLine(text); } |
Next
With the basics covered in this post you should be able to automate the Playwright browser for your first tests. Next week we look at Codegen and how we can use it to create selectors for us.
1 thought on “Automate Browsers With Playwright”