Puppeteer元素点击坐标flakiness

时间:2018-04-27 15:53:58

标签: javascript puppeteer

这让我绝对疯狂。以下是规范的摘录......

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次?!

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

1 个答案:

答案 0 :(得分:0)

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

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

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