尝试用.click()触发无onclick事件的Medium点赞按钮失败求助
Hey there! Let's break down why your attempt to click Medium's clap button with .click() isn't working, and how to fix it.
First: Double-Check You're Targeting the Right Element
Medium updates its UI class names from time to time, so clapButton might not be the correct identifier anymore. Here's how to verify:
- Right-click the clap button on any Medium article and select Inspect (Chrome/Firefox).
- Look at the element's class list in the DevTools panel. Current common class names for the clap button include
js-clap-buttonorclap-button(the exact name might vary slightly). - Replace your selector with the correct class, e.g.:
let btn = document.querySelector('.js-clap-button'); // querySelector is cleaner for single-element targeting
Why .click() Might Not Work Even With the Right Element
Most modern websites (including Medium) don't rely on inline onclick attributes anymore. Instead, they use:
- Event listeners added via
addEventListener() - Event delegation (events attached to parent elements, not the button itself)
- Some sites also add checks to ensure clicks are triggered by real users (e.g., checking
event.isTrusted, which returnsfalsefor synthetic.click()calls).
Fixes to Try
1. Simulate a Real Mouse Click
Instead of using .click(), create and dispatch a proper MouseEvent that mimics a real user's interaction:
const clapBtn = document.querySelector('.js-clap-button'); if (clapBtn) { const clickEvent = new MouseEvent('click', { bubbles: true, cancelable: true, view: window }); clapBtn.dispatchEvent(clickEvent); }
This replicates the full event flow a real click would trigger, which is more likely to be recognized by Medium's code.
2. Check for jQuery-Bound Events (If Applicable)
If Medium is using jQuery under the hood, you can try triggering the click via jQuery's trigger() method:
const clapBtn = $('.js-clap-button'); if (clapBtn.length) { clapBtn.trigger('click'); }
You can confirm jQuery is loaded by running typeof $ !== 'undefined' in the browser console.
Additional Checks
- Ensure you're logged in: Medium requires a signed-in account to clap for articles. If you're not logged in, the button won't respond even if you trigger the click correctly.
- Avoid automation spam: Medium might block repeated automated clicks, so don't run the code too frequently in a short time.
内容的提问来源于stack exchange,提问作者Gab De Jesus

