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

Protractor中isEnabled()返回undefined问题求助

Why does isEnabled() return undefined in Protractor?

The core issue here is that all Protractor element interaction methods (including isEnabled()) are asynchronous and return Promises, not immediate boolean values. Your current synchronous code isn't waiting for the Promise to resolve, so when you check us_dat.isEnabled() == false, you're actually comparing a pending Promise object to false—which evaluates to false, and in debugging, this might appear as undefined because the Promise hasn't completed yet.

Here's what's happening in your code:

  1. When dat > 24, you initialize us_dat to the first matching element.
  2. You call us_dat.isEnabled() which returns a Promise, not a boolean.
  3. Comparing this Promise to false gives false, so your code skips the if block where you'd switch to the second element.
  4. You then try to click the first element, which might be disabled, leading to unexpected behavior.

Fixed Code Using Async/Await

Protractor fully supports async/await (introduced in version 5), which makes handling asynchronous operations much cleaner. Here's your refactored code:

async function selectCalendarDate(dat) {
  // Start with the first matching date element
  let targetDateElement = element(by.xpath(`(//a[text()='${dat}'])[1]`));

  // For dates >24, check if the first element is enabled
  if (dat > 24) {
    const isElementEnabled = await targetDateElement.isEnabled();
    // Switch to the second element if the first is disabled
    if (!isElementEnabled) {
      targetDateElement = element(by.xpath(`(//a[text()='${dat}'])[2]`));
    }
  }

  // Wait for the click to complete and add a sleep (note: prefer explicit waits over sleep when possible)
  await targetDateElement.click();
  await browser.sleep(1000);
}

Key Changes Explained:

  • Wrapped the logic in an async function: This allows us to use await to pause execution until each Promise resolves.
  • Used await with isEnabled(): This waits for the Promise to resolve and gives us the actual boolean value (true or false) for our condition check.
  • Added await to browser.sleep(): Even sleep() returns a Promise, so we need to wait for it to complete too.
  • Simplified initial element selection: Since both branches of your original if/else started with the same element, we can initialize it once and only adjust it when needed.

Bonus Tip:

Avoid using browser.sleep() for waits whenever possible—instead, use Protractor's explicit waits (like browser.wait()) to wait for specific conditions (e.g., element to be clickable). This makes your tests more reliable and faster. For example:

const EC = protractor.ExpectedConditions;
await browser.wait(EC.elementToBeClickable(targetDateElement), 5000);
await targetDateElement.click();

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 06:28:58