简体   繁体   English

如何使用 puppeteer 在页面上注入查找元素

[英]How to get find element injected on a page using puppeteer

There is a browser extension that modifies the page and inserts its own html code into it, I get the html code of the page being checked, but the html code injection from the extension does not get into it, although if you run the check in the browser, then there is an injection.有一个浏览器扩展程序可以修改页面并将其自己的 html 代码插入其中,我得到了正在检查的页面的 html 代码,但是 html 代码虽然没有从检查扩展程序中运行浏览器,然后有一个注入。

those.那些。 after rendering the page, the extension integrates a block inside the page:渲染页面后,扩展在页面内部集成了一个块:

<div><div class="notification"><div class="notification__close"></div> <div class="notification__logo"></div> <div class="notification__title">
  </div> <div class="notification__cta">
    Activate
  </div></div> <!----></div>

When trying to catch this block, puppeteer does not find it:当试图捕捉这个块时,puppeteer 没有找到它:

let browser = await puppeteer.launch ({
      headless: false,
      ignoreDefaultArgs: true,
      args: [
        `--load-extension = $ {path.join (dir, '/extension')},`
      ]
    });
let page = await browser.newPage ();

await page.goto ('https://site.url', {waitUntil: 'networkidle2'});
await page.waitForSelector ('.notification__cta');
await page.click ('.notification__cta');

I get a dump on timeout when trying to find an item:: Timeout - Async callback was not invoked within the 30000 ms timeout specified by jest.setTimeout.尝试查找项目时出现超时转储:: Timeout - 在 jest.setTimeout 指定的 30000 毫秒超时内未调用异步回调。 Timeout - Async callback was not invoked within the 30000 ms timeout specified by jest.setTimeout.Error:超时 - 在 jest.setTimeout.Error 指定的 30000 毫秒超时内未调用异步回调:

If you look at the html that is stored in the page, then the notification block does not get there... Perhaps someone will have an idea how I can track html injections with a puppeteer and manipulate them?如果您查看存储在页面中的 html,那么通知块不会到达那里......也许有人会知道我如何使用木偶操纵者跟踪 html 注射并操纵它们?

I think you have a typo in your selector '. notification__cta'我认为您的选择器'. notification__cta' '. notification__cta' There should be no space between the dot and the classname. '. notification__cta'点和类名之间不应有空格。 Does it work when you change it to '.notification__cta' ?将其更改为'.notification__cta'是否有效?

I think your extension is in the category of " Content Scripts ".我认为您的扩展属于“内容脚本”类别。 At the moment they are not supported with puppeteer: see in the Docs, Working with Chrome Extensions :目前 puppeteer 不支持它们:请参阅文档, 使用 Chrome 扩展

NOTE It is not yet possible to test extension popups or content scripts.注意 尚无法测试扩展弹出窗口或内容脚本。

solution in using query-selector-shadow-dom ( https://www.npmjs.com/package/query-selector-shadow-dom )使用 query-selector-shadow-dom 的解决方案( https://www.npmjs.com/package/query-selector-shadow-dom

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM