剧作家Python -录制视频新打开的页面

gk7wooem  于 2023-08-08  发布在  Python
关注(0)|答案(1)|浏览(104)

我是剧作家自动化和使用Python的新手。我被困在录制视频后,一个新的页面(在同一窗口中打开新标签)打开。视频仅在第一个选项卡中显示。

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()

字符串
请建议如何连续记录,或者是否有方法在打开的新选项卡上开始记录。在我的用例中,新的选项卡是作为第一页中的一个操作的结果而打开的(上面只是一个解释我的问题的示例脚本)

ghhaqwfi

ghhaqwfi1#

我不知道你的页面是什么,但实际上这是由剧作家自动完成的。
这里有一个例子:

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)

字符串
我们导航到Twitter,点击Cookie和策略链接(在新选项卡中打开),然后我们就等着。
在您的文件夹/videos中,您应该能够看到2个视频,一个用于第一页,另一个用于第二页。

相关问题