【问题标题】:What's puppeteer api for `document.elementFromPoint`?`document.elementFromPoint` 的 puppeteer api 是什么?
【发布时间】:2018-10-01 20:01:19
【问题描述】:

ElementFromPoint 在 MDN 中有描述,但我在 Puppeteer 中找不到类似的功能。

我知道我可以以page.$eval 执行我的代码,但我想知道是否有直接的page.elementFromPoint API。

MDN 文档链接:

https://developer.mozilla.org/en-US/docs/Web/API/DocumentOrShadowRoot/elementFromPoint

【问题讨论】:

    标签: javascript node.js dom chromium puppeteer


    【解决方案1】:

    目前还没有为DocumentOrShadowRoot.elementFromPoint()明确构建的Puppeteer函数,但是你可以使用page.evaluate()直接在页面DOM环境中执行这个功能:

    await page.evaluate( () => {
        const example = document.elementFromPoint( 100, 100 );
        example.style.color = '#f00';
    });
    

    如果您需要在特定坐标处点击或点按某个元素,可以使用mouse.click()touchscreen.tap()

    await page.mouse.click( 100, 100 );
    await page.touchscreen.tap( 100, 100 );
    

    【讨论】:

    • 带外部变量:await page.evaluate( (x, y) => { const example = document.elementFromPoint( x, y ); example.style.color = '#f00'; }, 100, 100);
    猜你喜欢
    • 1970-01-01
    • 2021-12-04
    • 2019-12-15
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多