【问题标题】:puppeteer: clicking button in shadowrootpuppeteer:单击 shadowroot 中的按钮
【发布时间】:2020-10-15 09:39:27
【问题描述】:

我在测试上下文中对 shadowroot 中的元素进行操作时遇到困难。假设我有一个 Web 组件 <my-component />,它包含一个按钮 <input id="my-button" type="submit" />

从 chrome 中的控制台,我可以执行以下操作:

document.getElementsByTagName('my-component')[0].shadowRoot.querySelector('#my-button').click()

我正在努力与 puppeteer 做同样的事情。

  it('should click the button', async () => {
    await page.goto(`https://localhost:${port}`, {
      waitUntil: ['networkidle0', 'load'],
    });

    await page.$eval('my-component', (el: Element) => {
      el.shadowRoot.querySelector('#my-button').click();
    });
  });

单击该按钮应该会向我的服务器发出一个 http 请求,该请求会检索一些我想在 dom 中断言的数据。该请求永远不会触发。有什么建议吗?

【问题讨论】:

    标签: typescript jestjs puppeteer web-component web-component-tester


    【解决方案1】:

    根据Puppeteer Team的这条评论,正确的方法是使用JS路径:

    在 Chrome 72(当前 Canary)中,我们引入了一个新选项 - “复制 JS 路径”,位于“复制选择器”选项旁边:

    使用JS路径的例子:

        it('should click the button', async () => {
          await page.goto(`https://localhost:${port}`, {
            waitUntil: ['networkidle0', 'load'],
          });
        
          const button = await (await page.evaluateHandle(`<JS-path-here>`)).asElement();
          button.click();
        });
    

    【讨论】:

    • 投反对票,因为作为开发人员,这个答案没有用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-07
    • 2021-08-30
    • 2018-11-05
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    相关资源
    最近更新 更多