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

如何在Protractor中每5秒刷新页面直至目标元素出现?

Solution: Refresh Page Every 5 Seconds Until Target Element Appears

Got it, let's fix this! The built-in Protractor Expected Conditions don't handle automatic page refreshes out of the box, but we can build a custom function to loop through checking for your element, refreshing the page if it's not found, until it shows up or we hit a total timeout limit.

Step-by-Step Implementation

First, let's create a reusable function that handles the refresh loop. Here's how you can adjust your code:

var EC = protractor.ExpectedConditions;
var targetEle = element(by.control({
    controlType: "sap.m.ObjectIdentifier", 
    id: /clone/, 
    properties: {text: "MANAGER"}
}));

// Custom function to wait for element with periodic page refreshes
async function waitForElementWithRefresh(element, refreshIntervalMs, totalTimeoutMs) {
    const startTime = Date.now();
    while (Date.now() - startTime < totalTimeoutMs) {
        try {
            // Check if the element is visible (half the refresh interval for quick checks)
            await browser.wait(EC.visibilityOf(element), refreshIntervalMs / 2);
            return; // Element found, exit the loop
        } catch (err) {
            // Element not found, refresh the page
            await browser.refresh();
            // Wait a short moment for the page to reload after refresh
            await browser.sleep(1000);
        }
    }
    // If we exit the loop, the element never appeared within the total timeout
    throw new Error(`Element not found within ${totalTimeoutMs}ms despite repeated refreshes`);
}

// Use the custom function: refresh every 5 seconds, total wait time 60 seconds
waitForElementWithRefresh(targetEle, 5000, 60000)
    .then(() => {
        console.log("Target element is now visible!");
        // Continue with your test steps here
    })
    .catch(err => {
        console.error(err.message);
    });

Key Details to Note

  • Async/Await: We use async/await to handle the asynchronous flow more cleanly (Protractor supports this in newer versions).
  • Refresh Logic: If the element isn't found within half the refresh interval, we trigger a page refresh and wait a short time for the page to reload before checking again.
  • Total Timeout: The function includes a total timeout (set to 60 seconds in the example) to avoid infinite loops if the element never appears.
  • Element Check: We use EC.visibilityOf to ensure the element is not just present in the DOM but also visible on the page—swap this with EC.presenceOf if you only need to confirm the element exists.

Why This Works Better Than Your Current Code

Your original code only waits 5 seconds once and then sleeps, but this solution keeps trying: it checks for the element, refreshes if needed, and repeats until the element shows up or the total timeout is reached.

内容的提问来源于stack exchange,提问作者Surendra Babu Gurram

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 22:52:27