如何使用 playwright-python 处理多个页面?

Posted

技术标签:

【中文标题】如何使用 playwright-python 处理多个页面?【英文标题】:How to handle multiple pages with playwright-python? 【发布时间】:2021-01-07 15:12:19 【问题描述】:

如何使用playwright-python 收听新页面?

javascript 中,它将被记录为:

const playwright = require("playwright");

(async () => 
  const browser = await playwright.chromium.launch();
  const context = await browser.newContext();
  const page = await context.newPage();
  context.on("page", async newPage => 
    console.log("newPage", await newPage.title())
  )

  // emulate some opening in a new tab or popup
  await page.evaluate(() => window.open('https://google.com', '_blank'))
  // Keep in mind to have some blocking action there so that the browser won't be closed. In this case we are just waiting 2 seconds.
  await page.waitForTimeout(2000)
  await browser.close();
)();

变成Python

from playwright import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(
        headless=False,
        executablePath='C:/Program Files/Google/Chrome/Application/chrome.exe'
    )
    context = browser.newContext()
    page = context.newPage()

'''
how to do in Python?
  context.on("page", async newPage => 
    console.log("newPage", await newPage.title())
  )

  // emulate some opening in a new tab or popup
  await page.evaluate(() => window.open('https://google.com', '_blank'))
'''

    page.waitForTimeout(2000)
    browser.close()

【问题讨论】:

这对github.com/microsoft/playwright-python/blob/…有帮助吗? 谢谢。在我的解决方案下方。 【参考方案1】:

感谢@hardkoded 这是解决方案:

from playwright import sync_playwright

def newPage(page):
   print("newPage() page title:", page.title())

with sync_playwright() as p:
    browser = p.chromium.launch(
        headless=False,
        executablePath='C:/Program Files/Google/Chrome/Application/chrome.exe'
    )
    context = browser.newContext()
    page = context.newPage()

    context.on("page", lambda page: newPage(page))

    page.evaluate('''() => 
        window.open('https://google.com', '_blank')
    ''')
       
    page.waitForTimeout(2000)
    browser.close()

【讨论】:

【参考方案2】:

如果您需要在没有事件监听器的情况下处理新页面(即通过单击链接打开新标签),您可以尝试以下代码:

    from playwright import sync_playwright

    with sync_playwright() as p:
        browser = p.chromium.launch()
        context = browser.newContext()
        page = context.newPage()
        page.goto('<site url>')
        with context.expect_page() as tab:
            page.click('.newTabByLink')
        # do some steps
        ...
        tab.close()
        
        browser.close()

【讨论】:

以上是关于如何使用 playwright-python 处理多个页面?的主要内容,如果未能解决你的问题,请参考以下文章

在 Python 的 Playwright 中,我如何获取与 ElementHandle 相关的元素(孩子、父母、祖父母、兄弟姐妹)?

微软出了一个Python小白神器!

微软开源最强Python自动化神器Playwright 不用写一行代码

微软开源最强Python自动化神器Playwright,不用写一行代码

微软开源最强Python自动化神器Playwright 不用写一行代码

Python程序员,你还在用selenium吗?试试Playwright吧