问题描述
如何使用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",'_blank'))
'''
page.waitForTimeout(2000)
browser.close()
解决方法
谢谢@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()
,
如果您需要在没有事件监听器的情况下处理新页面(即通过链接点击打开新标签页),您可以尝试以下代码:
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()