This is a follow up to this question.
The idea is to get where the user clicked in a Firefox browser through a JavaScript. Starting from the named question, the following JavaScript code almost does it:
var DocElements = document.getElementsByTagName('*');for(var i = 0; i < DocElements.length; i++){DocElements[i].onclick = function clicked(MyElement){var target = (MyElement.target) ? MyElement.target : MyElement.srcElement;var e = document.createElement('a'); e.setAttribute('id','myUniqueID'); e.setAttribute('value', this); target.appendChild(e);};}
This script inserts a child with id "myUniqueID" in any element clicked, then the script can retrieve the clicked documents by doing the following python code:
resultss=browser.driver.find_element_by_id("myUniqueID") #This uses selenium driver engine.
resultss.find_element_by_xpath("..") #This gets the parent node of selected one
The JS Code aims to pick any element clicked on the website, by appending itself to all the items in the html code:
document.getElementsByTagName('*')
But this script fails, because its not able to pick all the elements. For example, if you click in the month / gender form in google signup page,then select one month, it will pick the element that represents the selected month, but doesnt detect the first click on the form. Therefore, getElementsByTagName('*') is not picking all the elements in the website.
How can the Javascript be attached running for ALL the elements in a document, without exceptions?