Rayrun
← Back to Discord Forum

clear text

what is the best way to clear text ?

This thread is trying to answer question "What is the best way to clear text?"

3 replies
bububu8615
bububu8615

fill("")?

locator(..).clear()

ibrahim.bektas
ibrahim.bektas

await page.getByRole('textbox').clear(); Doc for that : https://playwright.dev/docs/api/class-locator#locator-clear

Answer

Sure, you want to clear text in an input field using Playwright. No problem! Here's how you do it.

First, you need to import the necessary modules from Playwright. In this case, we're using Chromium, but you can replace it with Firefox or WebKit if you're using those browsers.

import { chromium } from 'playwright';

Next, you'll launch a browser, create a new context, and open a new page.

const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();

Navigate to the page where your input field is. We'll use https://ray.run as an example.

await page.goto('https://ray.run');

Find your input element. You'll need to know its selector. In this example, we're using #my-input.

const inputElement = await page.locator('#my-input');

Now, here's the magic part. You can clear the text in the input field by using the fill method and passing an empty string as the value.

await inputElement.fill('');

Finally, don't forget to close the browser when you're done.

await browser.close();

And that's it! You've just cleared text in an input field using Playwright. Remember, if you're using a different browser, you'll need to adjust the import statement and possibly some of the code to match that browser's specific APIs. Happy testing!

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].