My goal is to capture user actions such as clicks, keypress, and other DOM events by adding JavaScript event listeners to the WebDriver instance. Everything works fine until I navigate to the next page, where I encounter an exception due to an undefined function caused by changes in the DOM. How can I effectively handle this exception?
Is there a method to store all events temporarily and retrieve them after all web page navigations have been completed?
Here's the code snippet:
((JavascriptExecutor) driver)
.executeScript("(function() {
var events = [];
window.addEventListener('click', function(e) {
events.push([+new Date(), 'click', [e.clientX, e.clientY], e.target.name, e.target.id]);
}, true);
window.addEventListener('keypress', function(e) {
events.push([+new Date(), 'keypress', e.target.name, e.target.id, String.fromCharCode(e.keyCode)]);
}, true);
window._getEvents = function() {
return events;
};
})();
");
response = (ArrayList)((JavascriptExecutor) driver)
.executeScript("return window._getEvents();");