Sure, you can use Playwright to perform a Google search! Here's how:
First, import the chromium
object from playwright
. Then, launch a new Chromium instance, create a new context, and open a new page.
import { chromium } from 'playwright';
(async () => {
const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();
Next, navigate to Google's homepage using the page.goto(url)
method.
await page.goto('https://www.google.com');
Now, fill in the search bar with your query using the page.fill(selector, value)
method and submit the form by pressing Enter.
await page.fill('input[name="q"]', 'Playwright testing framework');
await Promise.all([
page.waitForNavigation(),
page.press('input[name="q"]', 'Enter')
]);
Finally, get the search results and print the titles.
const results = await page.$$('div.g');
for (const result of results) {
console.log(await result.$eval('h3', el => el.textContent));
}
await browser.close();
})();
This script will print out all of the resulting titles on the first search engine results page. Remember, this is just one way to search on Google using Playwright. You can adapt this code to suit your specific needs. Happy testing!
If you still have questions, please ask a question and I will try to answer it.
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].