如何解决如何使用playwright-python处理多个页面?
如何使用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()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。