这让我发疯。以下是规范的摘录...
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"
运行我的规范来显示点击调试,当规范点击错误的元素时,调试器输出正确的坐标!我快疯了!
最佳答案
啊!从视口(viewport)中删除 isMobile
配置属性可以解决此问题!
page.setViewport({
width: 411,
height: 731,
deviceScaleFactor: 2.6,
isMobile: true, // <---------- REMOVE!
hasTouch: true,
isLandscape: false
});
我打开了这个错误报告:https://github.com/GoogleChrome/puppeteer/issues/2465
关于javascript - Puppeteer 元素点击坐标片状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50066129/