Chrome控制台调用DIV.click()返回undefined的问题及视频播放咨询
Hey there, let's break down your problem step by step:
1. Why does document.getElementById("flash").click() return undefined?
First off, this is NOT an error! As @PatrickEvans pointed out, the DOM click() method doesn't have a return value by design—so seeing undefined in the console is totally normal. It just means the method executed without producing a value, not that it failed to run. You can verify the element was found first by running const flashDiv = document.getElementById("flash"); if flashDiv isn't null, your element was selected and the click was triggered.
2. How to properly click this <div> tag
If document.getElementById("flash") successfully grabs the element, calling flashDiv.click() is technically correct, but it might not work as expected for a couple reasons:
- The div might not have a click event bound to it: Some sites don't attach play triggers directly to the parent div—instead, the logic is tied to the
<video>element inside the iframe. - Event blocking or anti-automation measures: Many video sites block script-initiated clicks to prevent automated playback, relying on real user interactions instead.
Try these two approaches to increase your chances of success:
- Simulate a real user click event: Instead of calling
click()directly, create and dispatch a proper mouse event that mimics a real user's action:const flashDiv = document.getElementById("flash"); if (flashDiv) { const realClick = new MouseEvent('click', { bubbles: true, cancelable: true, view: window, clientX: 100, // Add fake coordinates to mimic real mouse position clientY: 100 }); flashDiv.dispatchEvent(realClick); } - Target the video element inside the iframe directly: Since the video lives in an iframe, you'll need to access its content document first. Note this only works if the iframe shares the same origin as the main page:
const iframe = document.querySelector("#flash iframe"); if (iframe) { try { const video = iframe.contentDocument.querySelector("video"); if (video) { video.play().catch(err => console.log("Playback failed:", err)); } } catch (err) { console.log("Can't access iframe content (cross-origin restriction):", err); } }
3. Will clicking the div play the video? Does the iframe affect this?
It depends on two key factors:
- Same-origin policy: If the iframe's source is different from the main onitube.com page, browsers will block you from accessing its internal DOM. In this case, you can only try triggering the parent div's click and hope the site's frontend is set up to pass that action to the iframe.
- Anti-automation checks: Most video platforms detect script-generated interactions. If the site sees your click came from a console script instead of a real mouse, it might block playback—you might need to complete any on-page verification (like captchas) first, or stick to manual clicks for playback.
If clicking the div doesn't start the video, it's likely either the div has no play-triggering click event bound to it, or the site is blocking script-initiated actions.
内容的提问来源于stack exchange,提问作者Ibrahim D.




