Puppeteer Node.js报错invalid namespace,求排查Fortnite爬取程序问题
Hey Ben, let's work through this issue with your Puppeteer script for grabbing Fortnite level and XP. That "invalid namespace" error usually pops up from either a small code oversight, anti-scraping measures on Fortnite Tracker, or unreliable element waiting. Here's how to troubleshoot and fix it:
1. Fix the Obvious Typo First
Looking at your code snippet, you wrote documen... instead of document in the page.evaluate() callback. Even if this was just a copy-paste mistake when sharing, a typo like this would break the script and could throw namespace-related errors (since documen isn't a defined browser object). Make sure that line is fully written as document.
2. Bypass Fortnite Tracker's Anti-Scraping Measures
Many gaming stats sites detect Puppeteer's default headless mode and block automated requests. The "invalid namespace" error might be a side effect of being flagged as a bot. Try adding these configuration tweaks to your Puppeteer launch options:
const browser = await puppeteer.launch({ headless: true, args: [ '--no-sandbox', '--disable-setuid-sandbox', '--disable-blink-features=AutomationControlled' ] });
Then, before navigating to the page, set a realistic user-agent and disable the webdriver flag to avoid detection:
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'); await page.evaluateOnNewDocument(() => { Object.defineProperty(navigator, 'webdriver', { get: () => undefined, }); });
3. Replace Fixed Wait Time with Reliable Element Waiting
Using page.waitFor(1000) is risky—if the page takes longer to load, your script will try to access elements that don't exist yet, which can cause unexpected errors. Instead, wait for the specific stats elements you want to scrape. For example, if the level is in an element with class trn-profile-header__stat-value, use:
await page.waitForSelector('.trn-profile-header__stat-value');
Full Corrected Script Example
Here's how your script might look with all these fixes applied:
const puppeteer = require('puppeteer'); let scrape = async () => { const browser = await puppeteer.launch({ headless: true, args: [ '--no-sandbox', '--disable-setuid-sandbox', '--disable-blink-features=AutomationControlled' ] }); const page = await browser.newPage(); // Avoid bot detection await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'); await page.evaluateOnNewDocument(() => { Object.defineProperty(navigator, 'webdriver', { get: () => undefined, }); }); await page.goto('https://fortnitetracker.com/profile/pc/Twitch.BadGuyBen'); // Wait for the stats element to load instead of fixed time await page.waitForSelector('.trn-profile-header__stat-value'); const result = await page.evaluate(() => { // Make sure to use full 'document' object const levelElement = document.querySelector('.trn-profile-header__stat:nth-child(1) .trn-profile-header__stat-value'); const xpElement = document.querySelector('.trn-profile-header__stat:nth-child(2) .trn-profile-header__stat-value'); return { level: levelElement ? levelElement.textContent.trim() : 'Not found', currentXp: xpElement ? xpElement.textContent.trim() : 'Not found' }; }); await browser.close(); return result; }; scrape().then(console.log).catch(console.error);
Final Notes
- Quick heads up: Fortnite Tracker has an official API! Using that is way more reliable and avoids all the headaches of web scraping.
- If you still get namespace errors, try running the browser in non-headless mode (
headless: false) to inspect the page manually—you might catch client-side errors or see that elements are loading differently than expected.
内容的提问来源于stack exchange,提问作者Ben W

