0

While automating our e2e tests I've encountered a couple of elements that are clickable manually in IE11, but I cannot click them through Selenium, or through their click() method in JS (which they do have).

This made me wonder - how do you make such an element in the first place? Is it possible? Or am I missing something.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • Could it be because of `onmousedown`? – malarres Jun 12 '20 at 07:32
  • Does this answer your question? [Detect if button click real user or triggered by a script](https://stackoverflow.com/questions/14794380/detect-if-button-click-real-user-or-triggered-by-a-script) – Karan Jun 12 '20 at 07:37
  • You could override `click` method in `HTMLElement` (`HTMLElement.prototype.click = function () {};`), or just in some individual element as well. – Teemu Jun 12 '20 at 07:48

1 Answers1

0

You can Use event.type to detect if it was triggered by mouse or keyboard and only then execute your code.

document.getElementById('myButton').addEventListener('mousedown', function(){myFunction(event, this);});

document.getElementById('myButton').addEventListener('keypress', function(){myFunction(event, this);});

function myFunction(event, id) {
    if (event.type == 'mousedown') {
        // do something
        alert('the mousedown event');
        return;
    }
    if (event.type == 'keypress') {
        // do something else
        alert('the keypress event');
        return;
    }
}
<button id="myButton">Click Me</button>
Maher Fattouh
  • 1,742
  • 16
  • 24
  • OP didn't ask how to detect the event in the handler, they have asked, how to create an element which won't respond JS `.click()` method. – Teemu Jun 12 '20 at 07:39
  • That's correct, although what you showed achieves the same. Nevertheless, I feel like in my case at least the JS listener wasn't actively distinguishing between mouse and JS click. More it's like a bug in the browser that I'm trying to trigger to better understand it, I don't know. – sashoalm Jun 12 '20 at 11:33
  • reading around I don't think there's a perfect solution. seems like some solutions (like detecting mouse x and y) work in some browsers but not in others. – Maher Fattouh Jun 12 '20 at 14:07