How to check all checkboxes on a web page from the browser console.
Jithin Somaraj
Senior QA Engineer | Specializing in Automation & Manual Testing | API Testing | Performance Testing | Expert in Enhancing Software Quality & Reliability
Some of us may get frustrated with the large number of checkboxes that need to be clicked to proceed to the next page. This is particularly frustrating during testing, as it must be done repeatedly, consuming time and affecting productivity. Fortunately, we can address this issue from the browser console. Here’s how you can do it:
Step 1 - Open the Web Page
Navigate to the web page containing the checkboxes you need to check.
In the picture above, there are 100 checkboxes. Normally, I would have to click on each checkbox individually, which would require 100 clicks to select all the checkboxes on the page.
We can select all these checkboxes without having to click on each one individually.
Step 2 - Open the Browser Console
Press F12 or right-click on the page and select "Inspect," then go to the "Console" tab.
Copy paste the below code in the console tab
const inputs = document.querySelectorAll('input[type="checkbox"]');
for (let i = 0; i < 49; i++) {
setTimeout(function() {
inputs[i].click()
})};
领英推è
Step 3 - Run the Script
To run the script, simply press Enter key, and the code will automatically execute and check all the checkboxes for you
Also please keep it in mind:
This code assumes there are at least 100 checkboxes on the page. If there are fewer, it will throw an error.
The "setTimeout" function is used here to ensure that clicks are executed in quick succession but not all at once, which can be useful to avoid potential issues with too many simultaneous actions. However, without specifying a delay, the actual impact might be minimal.
If the intent is to click all checkboxes with no delay, you could simplify the code by removing "setTimeout" :
const inputs = document.querySelectorAll('input[type="checkbox"]');
for (let i = 0; i < inputs.length; i++) {
inputs[i].click();
}
Professional translator, consultant, editor and writer
3 个月Nope... Almost half of the boxes were checked in Firefix, but far from all (175 total). For Chrome, an error is returned: VM657:6 Uncaught TypeError: Cannot read properties of undefined (reading 'click') ???at <anonymous>:6:15