Yes, you can navigate through the browser history using Playwright. Playwright provides a number of methods for navigation including page.goBack()
and page.goForward()
which can be used to navigate through the browser history.
Here are examples in both Python and JavaScript:
Python:
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto('https://example.com')
# Now, let's navigate to another page
page.goto('https://google.com')
# You can go back to the previous page using goBack()
page.goBack()
# And you can move forward in the history using goForward()
page.goForward()
browser.close()
JavaScript:
const playwright = require('playwright');
(async () => {
const browser = await playwright.chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();
await page.goto('https://example.com');
// Now, let's navigate to another page
await page.goto('https://google.com');
// You can go back to the previous page using goBack()
await page.goBack();
// And you can move forward in the history using goForward()
await page.goForward();
await browser.close();
})();
These commands will navigate through the browsing history in the same way as the back and forward buttons in a browser. It's important to note that page.goBack()
and page.goForward()
return a promise which resolves to the main resource response. If the navigation happens to a different origin, the method will resolve to null
.
Also, keep in mind that if there is no previous or next page in the history, these methods won't do anything. You can check the history length with page.evaluate(() => window.history.length)
.