简体   繁体   English

这 2 个 puppeteer 功能是否等效?

[英]Are these 2 puppeteer functions equivalent?

Do these 2 pieces of code both select and and click the same element?这两条代码是否都 select 和单击相同的元素?

Are they practically equivalent?它们实际上是等效的吗?

await page.$eval('#loginForm > button', form => form.click());
const button = await page.$('#loginForm > button');
await button.click();

Not always.不总是。

The first code just uses Web-API HTMLElement.click() .第一个代码仅使用 Web-API HTMLElement.click()

The second uses a more complicated way :第二种使用更复杂的方式

This method scrolls element into view if needed, and then uses page.mouse to click in the center of the element.如果需要,此方法将元素滚动到视图中,然后使用 page.mouse 单击元素的中心。 If the element is detached from DOM, the method throws an error.如果元素与 DOM 分离,则该方法会引发错误。

I've read in various issues that users had different effects from these approaches in the various page contexts.我在各种问题中读到过,用户在各种页面上下文中对这些方法产生了不同的影响。 Sometimes one of them works properly while other does not.有时其中一个可以正常工作,而另一个则不能。

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

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