You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Greasemonkey对无ID、无Name属性的按钮实现自动点击

解决方案

  • 方案1:XPath精准定位(推荐)
    利用按钮内部唯一文本Überspringen做匹配,不会和同class的其他按钮冲突,代码如下:

    // 匹配包含Überspringen文本的按钮
    const targetBtn = document.evaluate(
      '//button[contains(., "Überspringen")]',
      document,
      null,
      XPathResult.FIRST_ORDERED_NODE_TYPE,
      null
    ).singleNodeValue;
    
    if (targetBtn) {
      targetBtn.click();
    }
    
  • 方案2:兼容动态加载场景
    如果按钮是页面加载后动态渲染的(比如单页应用),可以用轮询定时检测元素是否存在:

    let checkTimes = 0;
    const timer = setInterval(() => {
      const targetBtn = document.evaluate(
        '//button[contains(., "Überspringen")]',
        document,
        null,
        XPathResult.FIRST_ORDERED_NODE_TYPE,
        null
      ).singleNodeValue;
      if (targetBtn) {
        clearInterval(timer);
        targetBtn.click();
      }
      // 最多检测30次,避免无效轮询占用资源
      if (checkTimes > 30) clearInterval(timer);
      checkTimes++;
    }, 500);
    
  • 方案3:遍历筛选备选方案
    不习惯用XPath的话也可以先匹配同class按钮,再按文本筛选:

    const allMatchBtns = document.querySelectorAll('button.v-btn.amber.darken-1');
    for (const btn of allMatchBtns) {
      if (btn.textContent.trim().includes('Überspringen')) {
        btn.click();
        break;
      }
    }
    

内容的提问来源于stack exchange,提问作者DKay36

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 21:06:02