To automate https://www.google.com using Playwright with Java, you can perform the following basic steps:
1. Set up Playwright Environment
- First, you would need to install Playwright and configure it for your preferred programming language (Java, JavaScript/TypeScript, Python, etc.).
- Playwright provides browsers like Chromium, Firefox, and WebKit, which can be used to open and interact with Google.
- Playwright allows launching a browser instance in either headless mode (without GUI) or headed mode (with GUI).
- For automation, you typically start a new browser session to ensure a clean state.
- A BrowserContext is like a separate user profile, with its own cookies, cache, and session data.
- Using contexts helps in isolating tests so that actions on one context don’t affect another.
- For example, if you open Google in one context, it won’t share login data with another context unless you configure it.
- You instruct Playwright to open the URL https://www.google.com
- Playwright waits until the page loads and becomes interactive.
- Playwright uses selectors (like CSS selectors, XPath, text selectors) to find elements on a page.
- The Google search box is a standard input field, so automation involves locating it.
- After finding the search box, Playwright can simulate user actions: Typing text into the box (e.g., “Playwright automation”), Adding key presses like Enter to submit the search.
- Once Google displays results, Playwright can: Locate all result links, Click on the first result or verify that certain text is present, Extract text from search results for validation.
- Google search results often load dynamically, so Playwright’s auto-waiting ensures it waits for elements to be ready before interacting.
- For example, it won’t try to click a result before the page is fully interactive.
- You can automate actions beyond just searching: Navigate to the Images or News tab, open links in new tabs, validate page titles or URLs to confirm successful navigation.
- At the end of the automation, Playwright closes the browser or context to free resources.
<dependencies> <dependency> <groupId>com.microsoft.playwright</groupId> <artifactId>playwright</artifactId> <version>1.44.0</version> <!-- Use latest if available --> </dependency> </dependencies>
import com.microsoft.playwright.*; public class GoogleAutomation { public static void main(String[] args) { // Step 1: Launch Playwright and Browser try (Playwright playwright = Playwright.create()) { Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false)); // Step 2: Create a new browser context and page BrowserContext context = browser.newContext(); Page page = context.newPage(); // Step 3: Navigate to Google page.navigate("https://www.google.com"); // Step 4: Accept cookies if visible (optional step depending on location) Locator acceptButton = page.locator("button:has-text('I agree'), button:has-text('Accept all')"); if (acceptButton.isVisible()) { acceptButton.click(); } // Step 5: Type a query into the search box page.locator("[name='q']").fill("Playwright Java"); // Step 6: Press Enter to search page.keyboard().press("Enter"); // Step 7: Wait for results to load and print titles page.waitForSelector("h3"); // wait for results System.out.println("Top Search Results:"); for (Locator result : page.locator("h3").all()) { System.out.println(result.textContent()); } // Step 8: Close browser browser.close(); } } }
Step | Description |
---|---|
Playwright.create() | Initializes Playwright engine. |
browser.newContext() | Creates a new browser context (like an incognito window). |
page.navigate() | Navigates to the URL. |
page.locator("[name='q']") | Finds the search box on Google using the name attribute. |
keyboard().press("Enter") | Simulates pressing Enter to submit the form. |
locator("h3") | Search results on Google usually appear under <h3> tags. |
Suggested Posts:
1. BrowserContext in Playwright
2. Automate GET API in Playwright
3. Comma Selectors in Playwright
4. Handle Alerts in Playwright
5. Find Web Elements by XPath in Playwright