Click Browser Back and Forward Button in Playwright Java

Profile picture for user arilio666

In this article, we will see how to go back and forth on the web page. In Playwright java, this can be achieved using goBack() and goForward() methods.

goBack()

  • The goBack() method navigates to the previous page in the browsing history and returns the primary resource response.
  • If it can not go forward, it returns null.
await page.goBack();
await page.goBack(options);

options: Object (optional)

timeout: number (optional) - Maximum operation time in milliseconds, defaults to 30 seconds, pass 0 to disable the timeout. The default value can be changed by using the browserContext.setDefaultNavigationTimeout(), browserContext.setDefaultTimeout(), page.setDefaultNavigationTimeout() or page.setDefaultTimeout() methods.
waitUntil: (optional) - When to consider operation succeeded, defaults to loading. Events can be either:
domcontentloaded - consider an operation to be finished when the DOMContentLoaded event is fired.
load - consider an operation to be finished when the load event is fired.
networkidle - consider the operation to be finished when there are no network connections for at least 500 ms.
commit - consider operation to be finished when the network response is received and the document starts loading.
returns Promise<null|Response>#

goForward()

  • The goForward() method allows navigation to the next page in the browsing history and returns the main resource response.
  • If it cannot go forward, it returns null.
await page.goForward();
await page.goForward(options);

options: Object (optional)

timeout: number (optional) - Maximum operation time in milliseconds, defaults to 30 seconds, pass 0 to disable the timeout. The default value can be changed by using the browserContext.setDefaultNavigationTimeout(), browserContext.setDefaultTimeout(), page.setDefaultNavigationTimeout() or page.setDefaultTimeout() methods.
waitUntil: (optional) - When to consider operation succeeded, defaults to loading. Events can be either:
domcontentloaded - consider an operation to be finished when the DOMContentLoaded event is fired.
load - consider an operation to be finished when the load event is fired.
networkidle - consider an operation to be finished when there are no network connections for at least 500 ms.
commit - consider operation to be finished when the network response is received and the document starts loading.
returns Promise<null|Response>#

Example:

Let us search in programsbuzz the keyword "Playwright," When the search result page loads, let us go back to the page and go forward again.

public static void main(String[] args) {
        try (Playwright playwright = Playwright.create()) {
            Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
            Page page = browser.newPage();
            page.navigate("https://www.programsbuzz.com");
            page.locator("//i[@class='fas fa-search']").click();
            page.locator("//input[@id='edit-keys']").type("Playwright");
            page.locator("//input[@id='edit-submit']").click();
            page.goBack();
            page.goForward();
            browser.close();
            playwright.close();
        }
    }
}

Doing this will go back one step of the page, and the next, it will go forward to the same results page.