【问题标题】:how to use puppeteer-sharp touchStart and touchEnd and touch move如何使用 puppeteer-sharp touchStart 和 touchEnd 和 touch move
【发布时间】:2020-06-05 14:33:14
【问题描述】:

puppeteer-sharp API 中的触摸屏只提供了一种方法:TapAsync。但我需要拖动(touchStart 和 touchEnd 和 touch move)。我该怎么做?

【问题讨论】:

    标签: c# puppeteer puppeteer-sharp


    【解决方案1】:

    PuppeteerSharp 没有这些 API(因为 Puppppeteer 没有这些 API)。但是,您可以尝试以与 TapAsync 相同的方式将这些消息发送到浏览器。

    这是TapAsync 代码:

    // Touches appear to be lost during the first frame after navigation.
    // This waits a frame before sending the tap.
    // @see https://crbug.com/613219
    await _client.SendAsync("Runtime.evaluate", new RuntimeEvaluateRequest
    {
        Expression = "new Promise(x => requestAnimationFrame(() => requestAnimationFrame(x)))",
        AwaitPromise = true
    }).ConfigureAwait(false);
    
    var touchPoints = new[] { new TouchPoint { X = Math.Round(x), Y = Math.Round(y) } };
    await _client.SendAsync("Input.dispatchTouchEvent", new InputDispatchTouchEventRequest
    {
        Type = "touchStart",
        TouchPoints = touchPoints,
        Modifiers = _keyboard.Modifiers
    }).ConfigureAwait(false);
    
    await _client.SendAsync("Input.dispatchTouchEvent", new InputDispatchTouchEventRequest
    {
        Type = "touchEnd",
        TouchPoints = Array.Empty<TouchPoint>(),
        Modifiers = _keyboard.Modifiers
    }).ConfigureAwait(false);
    

    页面有一个公共属性Client。您可以使用该客户端来执行这些调用。

    您可以查看here,类型支持:touchStart、touchEnd、touchMove 和 touchCancel。

    【讨论】:

    • 谢谢。我还有一个问题,如何通过链接实现功能:medium.com/@jsoverson/…。我需要拦截链接,但链接结束后立即启动 javascript 导航。这是一个 chrome 错误
    猜你喜欢
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-16
    • 2012-06-05
    相关资源
    最近更新 更多