I'm new to playwright automation and using Python. I'm stuck at recording video after a new page( opened in same window as new tab) is opened. The video is only showing for the first tab.
def test_perform_actions_and_record():
with sync_playwright() as playwright:
# Launch the browser and open example.com in the first page
browser = playwright.chromium.launch(headless=False)
context = browser.new_context(record_video_dir="videos/")
page = context.new_page()
page.goto("https://example.com")
# Open a new tab (a new page within the same context)
new_page = context.new_page()
context = browser.new_context(record_video_dir="videos/")
page = context.new_page()
# Go to example2.com in the new tab
new_page.goto("https://example2.com")
# Perform actions on the new tab
new_page.fill("#username", "example_user")
new_page.fill("#password", "example_password")
new_page.click("#login_button")
# Continue with other interactions on the new tab
# Close both pages and the context when done
page.close()
new_page.close()
context.close()
# Close the browser
browser.close()
Please suggest how to continuously record or even if there is a way to start recording on the new tab opened.
In my use-case the new tab is opened as a result for an action in the first page (The above is only a sample script to explain my problem)
I do not know what is your page, but actually this is done automatically by playwright.
Here you have an example:
from playwright.sync_api import sync_playwright
with sync_playwright() as playwright:
browser = playwright.chromium.launch(headless=False)
context = browser.new_context(record_video_dir="videos/")
page = context.new_page()
page.goto("https://twitter.com/")
# With following 3 lines we are expecting a new page to be open.
# Once the event of new page is fired, we catch that page and we save in new_page_info.
with context.expect_page() as new_page_info:
page.wait_for_timeout(5000)
page.click("//span[text()='Cookie Policy']") # Opens a new tab
# Once we have the event of new page, we get the attribute "value", which is the actual page
new_page = new_page_info.value
new_page.wait_for_timeout(5000)
We are navigating to twitter, clicking on Cookie and policy link (Which is open in a new tab) and then we just wait.
In your folder /videos, you should be able to see 2 videos, one for the first page and another one for the second page.