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

如何使用Intern Leadfoot waitForDeletedByXpath函数及适用场景解析

How to Use Intern Leadfoot's waitForDeletedByXpath Function & Its Practical Use Cases

Let me clear up the common misunderstanding first: you’re right to be confused—this method does not wait for an element to become invisible; it waits for the element to be completely removed from the DOM tree. That’s a key distinction from elements that are just hidden (via display: none, visibility: hidden, or similar styles).

Basic Usage Example

Here’s how to implement it for your specific loading spinner scenario:

// Assume your search button and loading spinner have these XPaths
return this.remote
  // Trigger the search action
  .findByXpath('//button[@id="search-submit"]')
  .click()
  .end()
  // Wait up to 5 seconds for the loading spinner to be deleted from DOM
  .waitForDeletedByXpath('//div[@class="loading-spinner"]', 5000)
  .then(() => {
    // Now the spinner is gone—safe to interact with the data grid
    return this.remote.findByXpath('//table[@id="results-grid"]');
  })
  .catch(err => {
    // Handle timeout or unexpected errors here
    console.error('Loading spinner did not get deleted in time:', err);
    throw err;
  });
  • The second parameter (5000) is an optional timeout in milliseconds; if omitted, it uses the waitForTimeout value from your Intern config.
  • The method will poll the DOM at intervals until the element is gone or the timeout hits.

Key Use Cases

This function shines in scenarios where you need to confirm an element is no longer part of the page structure entirely:

  • Your loading spinner scenario: Perfect when the spinner is removed from the DOM once data loads (not just hidden). If your spinner only gets hidden (style change), use waitForNotDisplayedByXpath instead.
  • Modal/popup cleanup: After closing a modal, wait for it to be deleted from the DOM to avoid accidental interactions with leftover elements.
  • Dynamic content removal: When deleting a list item or card, wait for the element to be fully removed before validating the updated list.
  • SPA route transitions: In single-page apps, wait for old page elements to be deleted after routing to ensure the new page is fully rendered.

Quick Troubleshooting Tip for Your Scenario

If waitForDeletedByXpath isn’t working for your loading spinner, open your browser’s DevTools:

  1. Trigger the search action
  2. Watch the loading spinner’s DOM entry—does it get removed (<div class="loading-spinner"> disappears entirely) or just hidden (styles change, but the element stays in the DOM)?
    If it’s just hidden, switch to waitForNotDisplayedByXpath instead.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 10:35:22