{"slug": "how-to-automate-publishing-to-csdn-and-wechat-mp-using-playwright-when-apis-fail", "title": "How to Automate Publishing to CSDN and WeChat MP Using Playwright (When APIs Fail)", "summary": "A developer automated article publishing to CSDN and WeChat MP using Playwright after CSDN deprecated its public Open API. The solution injects Markdown content into CSDN's dynamic editor via page.evaluate() and handles title input with keyboard simulation. The developer also fixed session log capture after a Claude Code update changed the log file path.", "body_md": "Today's focus was on automating article publishing to CSDN and WeChat MP (微信公众号) using Playwright, after CSDN deprecated its public Open API. Key achievements include: injecting Markdown content into CSDN's dynamic editor, handling title input quirks, implementing QR code login for WeChat MP, updating the Dev.to API publisher, and consolidating platform configs into a single YAML file. We also fixed session log capture after a Claude Code update changed the log file path.\n\n**Background**: In early 2026, CSDN silently shut down its public Open API. All endpoints returned 404/403. We needed a fallback to keep publishing to China's largest developer platform.\n\n**Solution**: Use Playwright to simulate a real user login and article creation. The approach:\n\n`csdn_cookies.json`\n\n.**Code snippet**:\n\n``` python\nimport asyncio\nfrom playwright.async_api import async_playwright\n\nasync def publish_to_csdn(title: str, content_md: str):\n    async with async_playwright() as p:\n        browser = await p.chromium.launch(headless=True)\n        context = await browser.new_context(storage_state=\"csdn_cookies.json\" if exists else None)\n        page = await context.new_page()\n        await page.goto(\"https://mp.csdn.net/mp_blog/creation/editor\")\n        # Inject content\n        await page.evaluate(f'''() => {{\n            const editor = document.querySelector('.editor-content');\n            if (editor) {{\n                editor.innerHTML = `{escaped_content}`;\n                editor.dispatchEvent(new Event('input', {{ bubbles: true }}));\n            }}\n        }}''')\n        # Fill title\n        await page.fill('#title-input', title)\n        await page.click('button:has-text(\"发布\")')\n        await page.wait_for_url(\"**/mp_blog/manage/article*\")\n        if not exists:\n            await context.storage_state(path=\"csdn_cookies.json\")\n        await browser.close()\n```\n\n**Result**: First run requires manual QR scan; subsequent runs are fully automated. The browser approach is 3–5 seconds slower than an API call, but it works.\n\n**Problem**: CSDN's Markdown editor is not a simple `<textarea>`\n\n. It's a nested rich-text component with shadow DOM and dynamic elements. `page.fill()`\n\nand `page.type()`\n\nfailed to inject content correctly.\n\n**Root Cause**: The editor uses `contenteditable`\n\nbut its state is managed by a frontend framework (Vue/React). Direct fill doesn't trigger the internal state update.\n\n**Solution**: Use `page.evaluate()`\n\nto set `innerHTML`\n\nand manually dispatch an `input`\n\nevent. For the title input, first focus, then simulate typing with `page.keyboard.type()`\n\nwith a delay.\n\n```\nawait page.click('#title-input')\nawait page.wait_for_timeout(300)\nawait page.keyboard.type(title, delay=50)\n```\n\n**Result**: Content and title injection now works reliably over 10 consecutive tests.\n\n**Background**: After upgrading to Claude Code 2.1.143, our session capture hook found no data in `~/.claude/history.jsonl`\n\n.\n\n**Root Cause**: Version 2.1.143 moved per-project logs to `~/.claude/projects/<project-name>/logs/`\n\n.\n\n**Solution**: Update the hook to check the new path first, with a fallback to the old path. Also detect version to decide.\n\n``` python\nimport pathlib\nimport subprocess\n\ndef get_history_path():\n    version = subprocess.run([\"claude\", \"--version\"], capture_output=True, text=True).stdout\n    if parse_version(version) >= (2, 1, 143):\n        return pathlib.Path.home() / \".claude\" / \"projects\" / get_current_project() / \"logs\"\n    else:\n        return pathlib.Path.home() / \".claude\" / \"history.jsonl\"\n```\n\n**Result**: Session capture works again without data loss.\n\n**Chosen**: Playwright for browser automation.\n\n**Alternatives**: Selenium WebDriver + ChromeDriver.\n\n**Why**:\n\n`time.sleep()`\n\n.**Trade-off**: Larger package size (≈100MB), less team familiarity. But stability wins.\n\n**Chosen**: Store all platform settings (publisher class, cookie file, selectors, endpoints) in `platforms.yaml`\n\n.\n\n**Alternatives**: Hardcode configs or use environment variables.\n\n**Why**:\n\n```\nplatforms:\n  csdn:\n    publisher_class: publishers.csdn.CSDNPublisher\n    login_url: \"https://passport.csdn.net/login\"\n    editor_url: \"https://mp.csdn.net/mp_blog/creation/editor\"\n    cookie_file: \"csdn_cookies.json\"\n  wechat_mp:\n    publisher_class: publishers.wechat_mp.WeChatMPPublisher\n    login_qrcode_selector: \"#login-qrcode\"\n    cookie_file: \"wechat_cookies.json\"\n```\n\n**Trade-off**: Requires validation and error handling, but long-term maintenance is easier.\n\n**Chosen**: Use Playwright to automate WeChat MP login via QR code scanning, then cache cookies.\n\n**Alternatives**: Unofficial APIs (risky, may be banned).\n\n**Why**:\n\n**Trade-off**: Requires human intervention on first run. But can be mitigated by notification to ops team.\n\nToday's work proves that multi-platform publishing is feasible even without open APIs. The key is building flexible and resilient automation that can adapt to real-world changes.", "url": "https://wpnews.pro/news/how-to-automate-publishing-to-csdn-and-wechat-mp-using-playwright-when-apis-fail", "canonical_source": "https://dev.to/quarktimes/how-to-automate-publishing-to-csdn-and-wechat-mp-using-playwright-when-apis-fail-4g01", "published_at": "2026-06-15 03:05:28+00:00", "updated_at": "2026-06-15 03:10:39.039995+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["CSDN", "WeChat MP", "Playwright", "Claude Code", "Dev.to"], "alternates": {"html": "https://wpnews.pro/news/how-to-automate-publishing-to-csdn-and-wechat-mp-using-playwright-when-apis-fail", "markdown": "https://wpnews.pro/news/how-to-automate-publishing-to-csdn-and-wechat-mp-using-playwright-when-apis-fail.md", "text": "https://wpnews.pro/news/how-to-automate-publishing-to-csdn-and-wechat-mp-using-playwright-when-apis-fail.txt", "jsonld": "https://wpnews.pro/news/how-to-automate-publishing-to-csdn-and-wechat-mp-using-playwright-when-apis-fail.jsonld"}}