【问题标题】:How do i click on a specific element from a list of elements如何从元素列表中单击特定元素
【发布时间】:2020-04-07 13:27:23
【问题描述】:

我有一个循环遍历页面上所有用户的代码,我希望它查找特定用户,然后单击该用户以从用户那里检索其他信息。我该怎么做?

const item_Container = await page.$$(".user");
await page.waitFor(1000)
for (const items of item_Container) {
    const item_Name = await items.$(".user__title");
    const name = await items.$eval(".user__title a", e => e.innerHTML);
    let ans = name.includes(kw);
    console.log(ans);


}

【问题讨论】:

    标签: javascript node.js puppeteer webautomation


    【解决方案1】:

    找到元素后,您可以通过模拟鼠标单击来单击它:

    await items.click();
    

    或 HTMLElement.click:

    await page.evaluate(el => el.click(), items);
    

    根据我的经验,有时模拟鼠标点击不起作用(元素被另一个元素覆盖),在这种情况下HTMLElement.click 会派上用场。

    【讨论】:

      猜你喜欢
      • 2017-03-03
      • 1970-01-01
      • 2019-05-10
      • 1970-01-01
      • 1970-01-01
      • 2020-10-08
      • 1970-01-01
      • 2012-02-07
      • 2013-01-19
      相关资源
      最近更新 更多