Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all Elements on a page [closed]

Tags:

javascript

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.

like image 453
John Avatar asked May 13 '13 08:05

John


People also ask

Which select all the elements on the page?

The * selector selects all elements.

How do you select all elements of a website?

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.

What does the querySelectorAll () method do?

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.

How do you select all elements in DOM?

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.


1 Answers

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);
}
like image 60
JasperV Avatar answered Nov 07 '22 20:11

JasperV