{"record":{"id":"f725bdd8cd068f0d","repo":"bevyengine/bevy","slug":"cannot-find-element-selector","errorCode":null,"errorMessage":"Cannot find element: {selector}.","messagePattern":"Cannot find element: (.+?)\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/bevy_winit/src/winit_windows.rs","lineNumber":284,"sourceCode":"        )]\n        let mut winit_window_attributes = winit_window_attributes.with_title(window.title.as_str());\n\n        #[cfg(target_arch = \"wasm32\")]\n        {\n            use wasm_bindgen::JsCast;\n            use winit::platform::web::WindowAttributesExtWebSys;\n\n            if let Some(selector) = &window.canvas {\n                let window = web_sys::window().unwrap();\n                let document = window.document().unwrap();\n                let canvas = document\n                    .query_selector(selector)\n                    .expect(\"Cannot query for canvas element.\");\n                if let Some(canvas) = canvas {\n                    let canvas = canvas.dyn_into::<web_sys::HtmlCanvasElement>().ok();\n                    winit_window_attributes = winit_window_attributes.with_canvas(canvas);\n                } else {\n                    panic!(\"Cannot find element: {selector}.\");\n                }\n            }\n\n            winit_window_attributes =\n                winit_window_attributes.with_prevent_default(window.prevent_default_event_handling);\n            winit_window_attributes = winit_window_attributes.with_append(true);\n        }\n\n        let winit_window = event_loop.create_window(winit_window_attributes).unwrap();\n        let name = window.title.clone();\n        prepare_accessibility_for_window(\n            event_loop,\n            &winit_window,\n            entity,\n            name,\n            accessibility_requested.clone(),\n            adapters,\n            handlers,","sourceCodeStart":266,"sourceCodeEnd":302,"githubUrl":"https://github.com/bevyengine/bevy/blob/396ca727080776bd313bb892423b7d94e03b81b4/crates/bevy_winit/src/winit_windows.rs#L266-L302","documentation":"The wasm counterpart of a valid-but-unmatched selector: in winit_windows, document.querySelector succeeded syntactically but returned null, so bevy panics \"Cannot find element: {selector}.\". The selector is fine; the HTML document simply contains no element matching it at the time the window was created.","triggerScenarios":"Window { canvas: Some(\"#game-canvas\".into()) } while index.html has no <canvas id=\"game-canvas\">; a typo'd or renamed id; the wasm module executing before the DOM is parsed (script in <head> without defer); canvas existing only after JS dynamically inserts it later.","commonSituations":"Web builds where the script tag placement changed; renaming the canvas in HTML but not in Rust (or vice versa); frameworks that render the host page asynchronously; copy-pasting index.html templates that use a different id.","solutions":["Add the matching element to the HTML host page: <canvas id=\"game-canvas\"></canvas>.","Load the wasm binary after DOM readiness — put the script at the end of <body> or use defer/module scripts.","Double-check exact spelling and case of the id/class on both sides.","If the canvas is inserted dynamically, wait for it to exist before spawning the Window (e.g. gate app start on a JS readiness signal)."],"exampleFix":"<!-- before: index.html missing the element -->\n<body><script src=\"pkg/my_game.js\"></script></body>\n\n<!-- after: canvas exists and script runs after it -->\n<body>\n  <canvas id=\"game-canvas\"></canvas>\n  <script type=\"module\" src=\"pkg/my_game.js\"></script>\n</body>","handlingStrategy":"validation","validationCode":"#[cfg(target_arch = \"wasm32\")]\nfn canvas_exists(selector: &str) -> bool {\n    web_sys::window()\n        .and_then(|w| w.document())\n        .and_then(|d| d.query_selector(selector).ok().flatten()) // Ok(None) = no match\n        .is_some()\n}\n\n// gate window creation on DOM readiness\nif !canvas_exists(\"#game-canvas\") {\n    web_sys::console::warn_1(&\"#game-canvas missing — check index.html\".into());\n}","typeGuard":null,"tryCatchPattern":"// Panic happens during window creation; probe the DOM first and defer the Window\n// (or fall back to Bevy-created canvas) when the element is absent.","preventionTips":["Load the wasm module after the canvas element (end of body, defer, or module script).","Keep the canvas id in one place; update HTML and Rust together.","For dynamically inserted canvases, start the app from a DOM-ready callback."],"tags":["rust","bevy","wasm","windowing","dom","html","panic"],"backgroundTag":"dom-element-not-found","analyzedSha":"396ca727080776bd313bb892423b7d94e03b81b4","analyzedAt":"2026-08-20T16:12:39.808Z","contentChangedAt":"2026-08-20T16:12:39.808Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}