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())
Posts You Might Like
- Why playwright is better than selenium webdriver, is it?
- Handle dropdowns in Playwright Python
- Open the browser and Close in Playwright Python
- Handle checkbox in Playwright Python
- Handle IFrames in Playwright Python
- Element Operations in Playwright Python
- Page level commands in Playwright Python
- Element State with Playwright Python