×
Navigation Commands Playwright Python

Navigation commands in Playwright python

Written by


We use goto() method to visit a web page. Similarly, when we perform some actions like a click we navigate to different pages. These are stored in browser history. We can use this browser history to navigate back and forth or reload/refresh the page the page.

Let’s see what those methods are

Goto:

Take you to the web page which we have given.

from time import sleep
from playwright.sync_api import sync_playwright
playwright = sync_playwright().start()
browser = playwright.chromium.launch(headless=False)
page = browser.new_page()
page.goto("https://google.com")

Navigate Back

Takes the user to the previous page, this will not work no page has been navigated before this command

page.go_back()

Navigate Forward

Takes the user to the next page, to use this you must have used go_back() at least once

page.go_forward()

Refresh the page:

Refreshes the current page, this will be useful some element is not loaded

page.reload()

The complete code:

from time import sleep
from playwright.sync_api import sync_playwright
playwright = sync_playwright().start()
browser = playwright.chromium.launch(headless=False)
page = browser.new_page()
page.goto("https://google.com")
print(page.title())
sleep(2)
page.locator("(//a[@class='pHiOh'])[1]").click()
print(page.title())
page.go_back()
print("Going back")
print(page.title())
page.go_forward()
print("Going forward")
print(page.title())
page.reload()
print("Refreshing")
print(page.title())
Navigation Commands Playwright Python Testercoder

Author :

Senior Software Engineer in Test, with more than a decade of experience in automation tools like Selenium, Playwright, Protractor, and Puppeteer with Python, Java, and JS respectively.