使用javascript点击whatsapp web中的按钮

thtygnil  于 2023-02-21  发布在  Java
关注(0)|答案(1)|浏览(171)

我有下面的代码,我想做的是将鼠标悬停在msg-container(message,在whatsapp web上)上,点击出现的三角形上下文按钮,然后点击“下载”
我正在尝试自动下载语音消息(只有消息上有下载按钮是语音消息或媒体,我想,我也执行此代码时,“星号消息”选项卡打开)
但由于某种原因,它只是悬停在消息上,三角形出现,它点击了它,但没有点击下载按钮。
我知道这是非常具体的,但要重现这一点,你可以星星2或3语音消息在whatsapp网站,去星消息标签上的浏览器,打开控制台和粘贴代码。

var messageContainers = document.querySelectorAll('[data-testid="msg-container"]');
for (var i = 0; i < messageContainers.length; i++) {
  setTimeout(function(container) {
    // Simulate a mouseover event on the container element
    var mouseoverEvent = new MouseEvent('mouseover', {
      view: window,
      bubbles: true,
      cancelable: true
    });
    container.dispatchEvent(mouseoverEvent);

    // Wait for the "down context" button to appear, then simulate a click event on it
    setTimeout(function() {
      var tri = container.querySelector('[data-testid="icon-down-context"]');
      if (tri) {
        var clickEvent = new MouseEvent('click', {
          view: window,
          bubbles: true,
          cancelable: true
        });
        tri.dispatchEvent(clickEvent);

        // Wait for the download button to appear, then simulate a click event on it
        setTimeout(function() {
          var downloadButton = container.querySelector('[data-testid="mi-msg-download"]');
          if (downloadButton) {
            downloadButton.dispatchEvent(clickEvent);
          }
        }, 1000);
      }
    }, 1000);
  }, 1000 * i, messageContainers[i]);
}
7fyelxc5

7fyelxc51#

出于某种原因,这个起作用了。以防万一还有人需要它。

var messageContainers = document.querySelectorAll('[data-testid="msg-container"]');
for (var i = 0; i < messageContainers.length; i++) {
  setTimeout(function(container) {
    // Simulate a mouseover event on the container element
    var mouseoverEvent = new MouseEvent('mouseover', {
      view: window,
      bubbles: true,
      cancelable: true
    });
    container.dispatchEvent(mouseoverEvent);
    setTimeout(function() {
    // Find the triangle element and simulate a click event on it
        var tri = container.querySelector('[data-testid="down-context"]');
        if (tri) {
      
        var clickEvent = new MouseEvent('click', {
          view: window,
          bubbles: true,
          cancelable: true
        });
        tri.dispatchEvent(clickEvent);

        // Wait for the context menu to appear
        setTimeout(function() {
          // Find the download button and simulate a click event on it
          var downloadButton = document.querySelector('li[data-testid="mi-msg-download"] > div._1MZM5');
          if (downloadButton) {
            downloadButton.dispatchEvent(new MouseEvent('click', {
              view: window,
              bubbles: true,
              cancelable: true
            }));
          }
        }, 500);
        }
        },500);
  }, 1000 * i, messageContainers[i]);
}

相关问题