jackwener/OpenCLI · error · CliError

SELECTOR

SELECTOR

Error message

Could not find element: Trae CN new task button

What it means

A SELECTOR CliError (src/errors.ts:160) thrown by `trae-cn new` when the in-page clickNewTaskScript() cannot find the 'New Task' button in the Trae CN IDE's webview UI. The library throws it rather than clicking blindly, since the rest of the new-task workflow depends on this button.

Source

Thrown at clis/trae-cn/new.js:23

export const newCommand = cli({
  site: 'trae-cn',
  name: 'new',
  access: 'write',
  description: 'Start a new Trae CN task in the current workspace, optionally sending the first prompt',
  example: 'OPENCLI_CDP_ENDPOINT=http://127.0.0.1:39240 OPENCLI_CDP_TARGET=talk opencli trae-cn new "请执行你的任务" -f json',
  domain: 'localhost',
  strategy: Strategy.UI,
  browser: true,
  args: [
    { name: 'prompt', required: false, positional: true, help: 'Optional first prompt to send after creating the task' },
    { name: 'timeout', type: 'int', required: false, help: 'Max seconds to wait for a fresh task composer (default: 10)', default: 10 },
  ],
  columns: ['Status', 'Action', 'Workspace', 'Model', 'Agent', 'FreshTaskConfirmed', 'TurnsBeforeSubmit', 'Turns', 'ComposerReady', 'SubmitMode'],
  func: async (page, kwargs) => {
    const timeout = normalizeTimeout(kwargs.timeout, 10);
    const clicked = await page.evaluate(clickNewTaskScript());
    if (!clicked?.ok) {
      throw selectorError('Trae CN new task button');
    }

    const started = Date.now();
    let state = null;
    while (Date.now() - started < timeout * 1000) {
      await page.wait(0.5);
      state = await page.evaluate(currentTaskStateScript());
      if (state?.composerReady && state.turns === 0) break;
    }

    if (!state?.composerReady || state.turns !== 0) {
      throw new CommandExecutionError(
        `Clicked Trae CN new task via ${clicked.method}; fresh empty composer was not confirmed`,
        `Observed composerReady=${state?.composerReady ? 'yes' : 'no'}, turns=${state?.turns ?? 'unavailable'}. Verify the current window is a Trae CN chat workspace and retry.`,
      );
    }

    const turnsBeforeSubmit = state.turns;

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Update Trae CN automation selectors (clickNewTaskScript) to match the current IDE DOM — the hint says the page UI may have changed
  2. Ensure the Trae CN window is showing the chat/agent panel where the new-task button is visible before running the command
  3. Downgrade or align to a Trae CN version known to work with these selectors, or pin the IDE version
  4. Report the issue upstream (per the error hint) with the Trae CN version and a DOM snapshot

Example fix

// before
const clicked = await page.evaluate(clickNewTaskScript());
if (!clicked?.ok) throw selectorError('Trae CN new task button');
// after
const clicked = await page.evaluate(clickNewTaskScript());
if (!clicked?.ok) {
  await openSidebarPanel(page, 'chat'); // make sure panel is visible
  const retried = await page.evaluate(clickNewTaskScript());
  if (!retried?.ok) throw selectorError('Trae CN new task button');
}
Defensive patterns

Strategy: retry

Validate before calling

const btn = await page.evaluate(() => {
  const el = document.querySelector('[class*=new-task], [aria-label*=new task i], [title*=新建 i]');
  return !!el && el.getBoundingClientRect().width > 0;
});
if (!btn) throw new Error('New-task button not visible — open the chat panel first');

Type guard

function isNewTaskClicked(r) { return !!r && r.ok === true; }

Try / catch

let clicked = await page.evaluate(clickNewTaskScript());
for (let i = 0; !isNewTaskClicked(clicked) && i < 3; i++) {
  await page.wait(1);
  clicked = await page.evaluate(clickNewTaskScript());
}
if (!isNewTaskClicked(clicked)) throw selectorError('Trae CN new task button');

Prevention

When it happens

Trigger: `trae-cn new` runs page.evaluate(clickNewTaskScript()) and the script returns clicked.ok falsy — the new-task button selector matched nothing in the current Trae CN UI (wrong panel open, UI updated, or IDE not on the expected screen).

Common situations: Trae CN version update changed DOM classes/structure; the sidebar or agent panel is collapsed/hidden; the user is on a screen where the new-task button does not exist (e.g. welcome screen); language/locale variant renders different markup.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/275187c5fe987fdf. Report an issue: GitHub.