I am looking for a function in javascript which clicks on every element (links, buttons,...) on my page. All elements should be disabled by default. I am using this for my testing environment in Selenium to check whether all elements on my page are deactivated.
The * selector selects all elements.
getElementsByTagName() that will select all the instances of a certain HTML element on the current webpage based on its tag name, i.e. <div> . Calling document. getElementsByTagName("div") is all you need to do to select all <div> elements on the current page using JavaScript.
Document.querySelectorAll() The Document method querySelectorAll() returns a static (not live) NodeList representing a list of the document's elements that match the specified group of selectors.
To get all DOM elements by an attribute, use the querySelectorAll method, e.g. document. querySelectorAll('[class="box"]') . The querySelectorAll method returns a NodeList containing the elements that match the specified selector.
At first, get all elements on your page:
var elements = document.getElementsByTagName("*");
Now that you get them, make a mouse-event, make a loop and apply the event on every element:
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
for (var i=0; i < elements.length; i++)
{
elements[i].dispatchEvent (clickEvent);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With