【问题标题】:Puppeteer element click coordinates flakinessPuppeteer 元素点击坐标片状
【发布时间】:2018-04-27 15:53:58
【问题描述】:

这让我快疯了。以下是规范的摘录...

await page.goto('localhost:5000/settings?disable-sw-cache', {
  waitUntil: 'networkidle2'
});

// doesn't seem to help w/ stopping flakiness
await page.waitFor(2000); 

page.click("[data-puppeteer='reload-configuration-link']");
await page.waitForSelector('.mdc-snackbar.mdc-snackbar--active');

const message = await page.evaluate(
  selector => document.querySelector(selector).textContent,
  '.mdc-snackbar.mdc-snackbar--active'
);

expect(message).to.contain('Successfully reloaded device configuration');

此规范在 6 次中大约有 4 次通过。在失败的 2 次中,对 page.click 的调用是单击了错误的元素。我在点击事件中输出了坐标,它们在30,80,这是完全错误的。

当我记录应该点击的元素的坐标时...

await page.waitFor(2000); 

// log the current location of the element that is about to be clicked!
const coordinates = await page.evaluate(selector => {
  const element = document.querySelector(selector);
  const { x, y } = element.getBoundingClientRect();
  return [x, y];
}, "[data-puppeteer='reload-configuration-link']");

console.log(coordinates)

page.click("[data-puppeteer='reload-configuration-link']");

我得到64,194,这是正确的,但在下一行,page.click 有时会出于某种原因点击30,80

知道为什么这些坐标有 6 次中有 2 次是错误的吗?!


所以我用env DEBUG="puppeteer:mouse,puppeteer:keyboard" 运行我的规范来显示点击调试,当规范点击错误的元素时,调试器会输出正确的坐标!我快疯了!

【问题讨论】:

    标签: javascript puppeteer


    【解决方案1】:

    啊!从视口中删除 isMobile 配置属性可解决此问题!

    page.setViewport({
      width: 411,
      height: 731,
      deviceScaleFactor: 2.6,
      isMobile: true, // <---------- REMOVE!
      hasTouch: true,
      isLandscape: false
    });
    

    我打开了这个错误报告:https://github.com/GoogleChrome/puppeteer/issues/2465

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-08
      • 2019-07-01
      • 2020-01-01
      • 2010-09-06
      • 2021-05-31
      • 2015-11-20
      相关资源
      最近更新 更多