如何自动点击网站中指定人员的ASK按钮?
Got it, let's tweak your existing script to target only the ASK buttons linked to people whose names end with "KE" (case-insensitive, so it catches both JAKE and Spike). We'll keep all your original timeout settings and add a counter to maintain the 1-second interval between valid clicks.
Modified Code
function clickAsk(){ // Keep your existing click handler (updated to log the person's name instead of button HTML) $('button.ASK').on( 'click', function(){ const personName = $(this).closest('.position').find('.name').text().trim(); console.log('You clicked the ASK button for', personName); } ); let validButtonCounter = 0; // Counter to track only matching buttons for consistent timing $('button.ASK').each( function(){ // Get the associated person's name (adjust this selector to match your site's actual DOM structure!) const personName = $(this).closest('.position').find('.name').text().trim(); // Check if the name ends with "KE" (case-insensitive to cover JAKE, Spike, etc.) if (personName.toLowerCase().endsWith('ke')) { // Use the valid counter for timing instead of the global loop index setTimeout(() => { $(this).click(); }, validButtonCounter * 1000); validButtonCounter++; // Only increment for buttons that match our filter } } ); } // Keep your original initial delay setTimeout(clickAsk, 850);
Key Changes Explained
- Name Matching Logic: We use
toLowerCase().endsWith('ke')to make the check case-insensitive, so it works for uppercase, lowercase, and mixed-case names alike. - DOM Selector Adjustment: The line
$(this).closest('.position').find('.name').text()assumes your site's HTML structure looks something like this:
If your actual structure is different (e.g., the name is a direct sibling with a unique class), update this selector to correctly grab the person's name tied to each ASK button.<div class="position"> <span class="name">JAKE</span> <button class="ASK">ASK</button> </div> - Valid Button Counter: Instead of using the global
indexfrom theeachloop, we usevalidButtonCounterto only count buttons that meet our filter criteria. This ensures the 1-second interval between clicks stays consistent, even when we skip non-matching buttons. - Useful Console Log: The log now shows the person's name, making it easier to verify you're clicking the correct targets.
Quick Note
Double-check the selector for personName to match your site's actual DOM structure—this is the most critical part for the filter to work as expected. All your original timeout settings are preserved exactly as requested.
内容的提问来源于stack exchange,提问作者Fabian Montossi
相关产品推荐
相关产品推荐

