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

Java Selenium忽略加载spinner及脚本提速方案咨询

Selenium 脚本加载 spinner 优化及提速方案

关于是否可忽略 spinner 直接操作

该加载 spinner 通常伴随全屏透明遮罩层,z-index 高于商品列表元素,直接调用 Selenium 原生 click 方法会触发 ElementClickInterceptedException 报错,不建议完全忽略 spinner 直接操作。如果确实需要跳过等待 spinner 消失的步骤,可使用以下两种实现方案:

  • 改用 JavaScript 执行点击:原生 JS 点击逻辑不受页面遮罩层限制,可直接触发目标元素的点击事件,无需等遮罩消失,示例代码如下:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", targetElement);
  • 调整等待条件:不用主动等待 spinner 消失,直接设置显式等待直到目标商品元素可点击,Selenium 会自动轮询,只要遮罩消失、元素就绪就会立刻执行操作,比固定等待效率更高:
wait.until(ExpectedConditions.elementToBeClickable(prodList.get(i))).click();

注意:如果页面商品数据是在 spinner 消失后才完成加载,提前操作可能会拿到错误信息或点击无效元素,需确认数据加载时机后再使用上述方案。

其他脚本提速优化方案

1. 配置 Chrome 启动参数,禁用非必要资源加载

启动 Chrome 时添加参数关闭图片、扩展、GPU 等非必要加载项,也可开启无头模式,大幅降低页面加载耗时:

ChromeOptions options = new ChromeOptions();
options.addArguments("--headless=new"); // 开启无头模式
options.addArguments("--disable-images"); // 禁用图片加载
options.addArguments("--disable-extensions"); // 禁用浏览器扩展
options.addArguments("--disable-gpu");
options.addArguments("--no-sandbox");
WebDriver driver = new ChromeDriver(options);

2. 移除隐式等待,统一使用显式等待

当前代码同时设置了10秒隐式等待和20秒显式等待,两者会叠加生效,导致等待时间翻倍。删除隐式等待配置,所有等待场景使用针对性的显式等待即可,可大幅减少无效等待时间。

3. 提前缓存商品链接,取消返回上一页操作

这是效率提升最明显的优化方案:当前逻辑每次进入详情页后返回列表页,都需要重新等待页面加载、spinner 消失,可在首次进入分类页时,就把所有需要操作的商品的 href 属性提前抓取缓存到列表中,后续直接循环访问商品链接,完全避开返回后的 spinner 等待。

4. 优化元素定位策略

当前使用的全文档扫描 XPath 可缩小查询范围,比如先定位左侧分类的父容器,再查找子元素,减少 DOM 扫描范围,提升元素查找速度。

优化后完整代码示例

import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;

public class TestCodeOptimized {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/chromedriver");
        // 配置Chrome启动参数
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless=new");
        options.addArguments("--disable-images");
        options.addArguments("--disable-extensions");
        options.addArguments("--disable-gpu");
        options.addArguments("--no-sandbox");
        WebDriver driver = new ChromeDriver(options);
        // 移除隐式等待,仅用显式等待
        driver.manage().window().maximize();
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        
        driver.get("https://www.ediblearrangements.com/fruit-arrangements");
        wait.until(ExpectedConditions.elementToBeClickable(By.id("imgClose"))).click();
        
        // 遍历分类
        List<WebElement> catList = driver.findElements(By.xpath("//div[@class='left-nav']//a[@ftype='id']"));
        for (int cat = 0; cat < 2; cat++) {
            // 点击分类
            driver.findElements(By.xpath("//div[@class='left-nav']//a[@ftype='id']")).get(cat).click();
            wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@id='divArrangementList']/ul/li")));
            
            // 提前缓存前3个商品链接
            List<String> prodUrls = new ArrayList<>();
            List<WebElement> prodList = driver.findElements(By.xpath("//div[@id='divArrangementList']/ul/li/descendant::a[1]"));
            for (int j = 0; j < 3; j++) {
                prodUrls.add(prodList.get(j).getAttribute("href"));
            }
            
            // 直接访问商品链接,无需返回上一页
            for (String url : prodUrls) {
                driver.get(url);
                String prodName = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("arrnamewithsup"))).getText();
                System.out.println("商品名称:" + prodName);
            }
        }
        driver.quit();
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 01:45:01