...
×

Navigation commands in Playwright python

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 :

Pavankumar Nagaraj is an automation testing expert with over 12 years of experience, specializing in tools like Selenium, Protractor, Puppeteer, and Playwright, and skilled in Java, Python, and JavaScript

Pavankumar Nagaraj
Pavankumar Nagaraj

Pavankumar Nagaraj is an automation testing expert with over 12 years of experience, specializing in tools like Selenium, Protractor, Puppeteer, and Playwright, and skilled in Java, Python, and JavaScript

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.