Tutorial 8 – Refresh page, Navigate backward/forward, keypress using Playwright-Python
What you will Learn:
Page refresh
Navigate backward
Navigate forward
Keypress
Code snippets
Page refresh
Sometimes, we may want to refresh a webpage during test execution. We can do so using ‘reload’ command. Let us put a hard wait time of 4 secs so that we can see the page getting refreshed
Run the script
You will notice that, once the webpage is loaded, there is a wait time of 4 secs following which the page refreshes
Navigate backward
Let us comment below 2 lines
In line#11, we are navigating to a url, let us print the page url (line#13)
See above. In line#18, we are clicking the ‘sign up’ link and navigating to another webpage, let us print the page url (line#19).
In line#22, we are navigating back to the previous page and then printing the page url (line#23).
Save, right click and click “Run ‘hellopw’” to execute test
Notice below that the 3 urls get printed
Navigate forward
Let us now navigate forward the print page url
Save, right click and click “Run ‘hellopw’” to execute test
Notice below that the 4 urls get printed
Keypress in Playwright-python
Launch https://the-internet.herokuapp.com/key_presses
Bring cursor in the highlighted text area and press ‘Tab’ key. We notice the text ‘You Entered: TAB’ written below the text field
Let us see how we can automate this use case.
Let us inspect the tet area, notice that it can be located using the ‘id #target’
Playwright has ‘press’ method that we can use to simulate pressing a key. Please refer the url for various keys that we can be used with this method https://playwright.dev/python/docs/api/class-keyboard#keyboard-press
We will identify ‘key Presses’ using its text (line#12)
We then select the text area using its css selector #target and call the ‘F1’ key (line#13)
Save and execute
Notice below that F1 key was pressed
Similarly, we can try with other keys.
Code snippet (refresh, navigate)
import time
from playwright.sync_api import Playwright, sync_playwright, expect
def run(playwright: Playwright) -> None:
browser = playwright.chromium.launch(slow_mo=3000)
context = browser.new_context()
page = context.new_page()
page.goto("https://www.selenium-tutorial.com/p/selenium-training")
print(page.url)
#time.sleep(4)
#page.reload()
page.locator("a:left-of(:text(\"Sign Up\"))").first.click()
print(page.url)
#Navigate back
page.go_back()
print(page.url)
# Navigate forward
page.go_forward()
print(page.url)
# ---------------------
context.close()
browser.close()
with sync_playwright() as playwright:
run(playwright)
Code snippet (keypress)
import time
from playwright.sync_api import Playwright, sync_playwright, expect
def run(playwright: Playwright) -> None:
browser = playwright.chromium.launch(headless=False, slow_mo=3000)
context = browser.new_context()
page = context.new_page()
page.goto("https://the-internet.herokuapp.com/")
page.locator("text=Key Presses").click()
page.press("#target", "F1")
# ---------------------
context.close()
browser.close()
with sync_playwright() as playwright:
run(playwright)
Thank you for reading!