{"record":{"id":"89bd8de343ac7924","repo":"jackwener/OpenCLI","slug":"could-not-open-wechat-image-upload-opened-reas","errorCode":null,"errorMessage":"Could not open WeChat image upload: ${opened?.reason || 'unknown error'}","messagePattern":"Could not open WeChat image upload: (.+?)","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/weixin/create-draft.js","lineNumber":160,"sourceCode":"            return { ok: true };\n        } catch (error) {\n            return { ok: false, reason: String(error && error.message || error) };\n        }\n    })()`);\n    if (!fallback?.ok) {\n        throw new CommandExecutionError(`WeChat image upload fallback failed: ${fallback?.reason || 'unknown error'}`);\n    }\n}\n\nasync function uploadContentImage(page, image) {\n    const previousKeys = await readCdnImageKeys(page);\n    const opened = await evaluate(page, `(() => {\n        var button = document.querySelector('#js_editor_insertimage');\n        if (!button) return { ok: false, reason: 'insert-image button not found' };\n        button.click();\n        return { ok: true };\n    })()`);\n    if (!opened?.ok) throw new CommandExecutionError(`Could not open WeChat image upload: ${opened?.reason || 'unknown error'}`);\n    await page.wait(1);\n\n    const selected = await evaluate(page, `(() => {\n        var item = document.querySelector('.js_img_dropdown_menu .tpl_dropdown_menu_item');\n        if (!item) return { ok: false, reason: 'upload menu item not found' };\n        item.click();\n        return { ok: true };\n    })()`);\n    if (!selected?.ok) throw new CommandExecutionError(`Could not select WeChat image upload: ${selected?.reason || 'unknown error'}`);\n    await page.wait(1);\n    await injectImageFile(page, image);\n\n    for (let attempt = 0; attempt < 15; attempt++) {\n        await page.wait(2);\n        const state = await evaluate(page, `(() => {\n            var previous = new Set(${JSON.stringify(previousKeys)});\n            var editor = document.querySelector('#ueditor_0');\n            var images = editor ? Array.from(editor.querySelectorAll('img')) : [];","sourceCodeStart":142,"sourceCodeEnd":178,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/weixin/create-draft.js#L142-L178","documentation":"Thrown by uploadContentImage when the in-page script cannot find and click the editor's insert-image button (#js_editor_insertimage). The script returns { ok: false, reason: 'insert-image button not found' } and this error surfaces that reason.","triggerScenarios":"The editor toolbar is not rendered (session expired or editor didn't finish loading), WeChat renamed/removed the #js_editor_insertimage element in a front-end update, the page is showing a modal or alert that blocks the toolbar, or evaluate ran on the wrong page/frame.","commonSituations":"WeChat editor UI updates changing toolbar ids; command invoked on a half-loaded page; expired session landing on a login-interstitial that lacks the toolbar; headless rendering skipping toolbar initialization.","solutions":["Re-login to WeChat and confirm the article editor fully loads (title textarea visible) before uploading images.","Check whether WeChat changed the insert-image button id and update the #js_editor_insertimage selector.","Add a retry/wait that polls for the button for several seconds before failing.","Dismiss any open modals/dialogs on the editor page before invoking the command."],"exampleFix":"// before\nvar button = document.querySelector('#js_editor_insertimage');\nif (!button) return { ok: false, reason: 'insert-image button not found' };\n// after\nvar button = document.querySelector('#js_editor_insertimage, [data-role=\"insertimage\"], .js_insertimage');\nif (!button) return { ok: false, reason: 'insert-image button not found' };\nbutton.scrollIntoView();\nbutton.click();","handlingStrategy":"validation","validationCode":"await page.wait(4);\nconst editorReady = await evaluate(page, '!!document.querySelector(\"textarea#title\") && !!document.querySelector(\"#js_editor_insertimage\")');\nif (editorReady !== true) throw new Error('editor toolbar not ready — cannot upload images');","typeGuard":"function isInsertResult(v) { return typeof v === 'object' && v !== null && typeof v.ok === 'boolean' && typeof v.reason === 'string'; }","tryCatchPattern":"try {\n    await createDraftCommand(opts);\n} catch (e) {\n    if (/Could not open WeChat image upload/i.test(e.message)) {\n        console.error('insert-image button unavailable:', e.message);\n        // re-login or update selector, then retry once\n    }\n    throw e;\n}","preventionTips":["Verify the editor (and toolbar) is fully rendered before image steps.","Watch for WeChat front-end updates that rename #js_editor_insertimage.","Dismiss modals/overlays before interacting with the toolbar.","Use multiple candidate selectors or text-based lookup for the button."],"tags":["wechat","dom-parsing","editor-ui","scraping"],"backgroundTag":"selector-not-found","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}