如何自动化批量删除网页上的项目

在许多在线应用程序中,我们有时会遇到需要批量删除项目的情况。手动一次次点击删除按钮,然后确认操作不仅繁琐,而且非常浪费时间。通过简单的JavaScript脚本,我们可以实现这一流程的自动化。本文将向你展示一个简单的方法来批量删除网页上的项目。

1. 分析网页结构

首先,我们需要对网页结构有所了解。以下是一个典型的删除按钮和确认删除的HTML结构:

删除按钮的HTML:

确认删除的HTML:

2. 脚本编写

基于以上的结构,我们可以编写以下的JavaScript脚本来自动删除:

let currentIndex = 0;

function deleteNext() {
    const deleteLinks = document.querySelectorAll('a.op[action="doDelete"]');

    if (currentIndex < deleteLinks.length) {
        deleteLinks[currentIndex].click();

        setTimeout(() => {
            const confirmButton = Array.from(document.querySelectorAll('div.toolBar a.button span')).find(span => span.textContent.includes("确认"));
            if (confirmButton) {
                confirmButton.click();
                setTimeout(deleteNext, 500);
            }
        }, 200);
    } else {
        console.log("所有内容都已被删除或没有可删除的项。");
    }
}

deleteNext();

此脚本首先会点击删除链接,然后等待200毫秒以确保确认框出现,接着点击确认按钮。这个过程会持续进行,直到所有可以删除的项目都被处理。

3. 如何使用

  1. 打开你需要批量删除项目的网页。
  2. 打开浏览器的开发者工具(通常是‘F12’或右键选择‘检查元素’)。
  3. 切换到“控制台”或“Console”选项卡。
  4. 粘贴上面的脚本并回车执行。

总结

这个方法为批量删除网页上的项目提供了一种简单且有效的方法。但请注意,使用前先备份数据,避免误删除重要信息。

打赏作者

《如何自动化批量删除网页上的项目》上的5个想法

  1. 如果网站使用了iframe来加载内容(这是一种在网页中嵌入另一个网页的技术),那么可能需要切换到那个iframe中来运行脚本。

    你可以在控制台的顶部,找一个下拉框,里面列出了所有可用的iframe,你可以尝试切换到不同的iframe并重新运行脚本。

    1. 对于非IT行业的从业人员来说是有些困难,不过只学会怎么使用还是可行的,平时也能很大的提升效率。
      比如我当天需要删除网页里的1084条内容,因为没有一键清空的功能,只能一条一条删除,鼠标要重复点2168次,枯燥而没有意义。

  2. java.sql.SQLException: ORA-02290: 违反检查约束条件 (SZQDMS.PT_BU_DEALER_STOCK_CHK2)
    如果出现上述报错,可以使用下述的代码,跳过违反检查约束条件的行继续执行。

    1. let currentIndex = 0;

      function deleteNext() {
      const deleteLinks = document.querySelectorAll(‘a.op[action=”doDelete”]’);

      // 如果已经超出范围,结束函数
      if (currentIndex >= deleteLinks.length) {
      console.log(“所有内容都已被删除或没有可删除的项。”);
      return;
      }

      deleteLinks[currentIndex].click();

      setTimeout(() => {
      const errorMsg = document.querySelector(‘div.msg’);
      if (errorMsg && errorMsg.textContent.includes(‘java.sql.SQLException: ORA-02290’)) {
      console.log(`Skipping index ${currentIndex} due to error: ${errorMsg.textContent}`);
      currentIndex++;
      deleteNext(); // Proceed to the next item
      } else {
      const confirmButton = Array.from(document.querySelectorAll(‘div.toolBar a.button span’)).find(span => span.textContent.includes(“确认”));
      if (confirmButton) {
      confirmButton.click();
      setTimeout(() => {
      currentIndex = 1; // 由于页面刷新,从新的第二行开始
      deleteNext(); // Reload the deleteLinks and start from the first unprocessed item
      }, 500);
      } else {
      console.log(`确认按钮未找到,跳过索引 ${currentIndex}`);
      currentIndex++;
      deleteNext(); // Proceed to the next item
      }
      }
      }, 200);
      }

      deleteNext();

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注