首页 > 解决方案 > 获取嵌套 HTML 中的 HTML 元素

问题描述

该页面具有以下结构:

<html>
 <body>
  ...
  <button class="myclass1" type="button">Continue1</button>
   ...
    #document
     <html>
      <body>
       <button class="myclass" type="button">Continue2</button>

如何单击按钮 Continue2?

我正在尝试使用expect-puppeteer。但是,如果您有 puppeteer 的解决方案,那么我会使用它。

await expect(page).toClick('button', { text: 'Continue2'}); // not work(Nested html)
await expect(page).toClick('button', { text: 'Continue1'}); // work

标签: javascripthtmlnode.jspuppeteer

解决方案


您需要先检测帧,然后使用它而不是page. 在 puppeteer 中:

const frame = page.frames().find(frame => frame.name() === 'iframe-1');
await frame.click('button.myclass');

推荐阅读