惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
H
Hackread – Cybersecurity News, Data Breaches, AI and More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
小众软件
小众软件
月光博客
月光博客
D
DataBreaches.Net
L
LangChain Blog
美团技术团队
S
SegmentFault 最新的问题
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
博客园 - 司徒正美
aimingoo的专栏
aimingoo的专栏
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
H
Help Net Security
阮一峰的网络日志
阮一峰的网络日志
Y
Y Combinator Blog
I
InfoQ
U
Unit 42
Microsoft Azure Blog
Microsoft Azure Blog
J
Java Code Geeks
博客园 - 三生石上(FineUI控件)
腾讯CDC
Martin Fowler
Martin Fowler

博客园 - 至尊王者

使用 Android Lua Helper 在VSCODE中调试安卓Lua应用 lua语言特性及用途 Ollvm混淆与反混淆: goron框架字符串加密的实现原理 Ollvm混淆与反混淆: Goron框架编译与使用 安卓使用OLLVM混淆.so reinterpret_cast和其它cast的区别 雷电9中使用Fiddler抓包 ubuntu 22.04离线安装cuda 11.7.1、cudnn 8.9.3.28、nccl 2.18.3、tensorrt 8.6.1 C/C++程序隐藏符号 配置ETH 2.0 同步节点 Nethermind & Prysm rust在windows上编译openssl mac 远程桌面 the user account did not work的解决办法 MacOS静态编译ffmpeg 4.4.1、x264、x265等编解码库的脚本 Linux、Windows静态编译ffmpeg 4.4.1、x264、x265等编解码库的脚本 Visual Studio 2022 Key 在Docker中安装宝塔面板 idea/clion行注释位置(不在首列,注释就靠近代码块) linux手动编译llvm/clang linux与windows双系统保持时间同步
Puppeteer: 等待打开一个新页面
至尊王者 · 2021-01-10 · via 博客园 - 至尊王者

对于点击 <a target='_blank'> 标签打开新 tab 页的场景,Puppeteer目前(2019-03,v1.13.0)没有现成的 API 支持。因此需要一些 walkaround 来解决。有几个方案。

提取 href,手动打开新 page 去访问

url = await page.evaluate('() => $("a").attr("href")')
detail_page = await browser.newPage()
# goto 带了 waitForNavigation 的作用
await detail_page.goto(detail_page_url)

使用点击,再去轮徇 pages

代码如下。这个方案的问题在于,拿到 detail_page 时并不知道页面是否 load 完成了,在这个时候调用 .waitForNavigation() 可能会超时报错(因为没有 load 事件被 fire)。如果页面有 AJAX 请求,你可能需要写额外的 waitForSelector 来确保你要的数据已经在页面上。

# 点击完后出现新 tab 页
await page.click(f'#panel-5 tr:nth-child({index + 1}) a')

# 等新 Tab 页 ready,即 pages 中有新 tab 页。由于没有现成 API,只能靠等
detail_page = None
for i in range(5):
    pages = await browser.pages()
    try:
        detail_page = next(page for page in pages if 'biangeng.html' in page.url)
    except StopIteration:
        await asyncio.sleep(1)
    else:
        break
if detail_page is None:
    msg = "New page did not show up or show up so slowly."
    logger.error(msg)
    raise Exception(msg)