I want to write some automation to website. Simply filling in forms and simulating clicks on elements. But for some elements JavaScript function click() doesn't work. For example it is on this page: http://www1.plus.pl/bsm/ If I do click() on "Wyślij" button, nothing happens. It is my command:
document.getElementsByClassName('tab-button')[0].click()
What is wrong? Why on other elements this function works?
With yours help I found the solution:
var evt = document.createEvent('MouseEvents')
evt.initMouseEvent('mousedown', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
document.querySelectorAll('.tab-button')[0].dispatchEvent(evt)
Notice that it should be mousedown
event rather than click
. Some sites are done in a different way than others. Another important thing is 3rd parameter. It should be set to false
(in this particular case). It sets cancelable
value. Without this set to false
it doesn't work.
Thank you for all answers!
document.getElementsByClassName('tab-button')[0].dispatchEvent(event)
or
document.getElementsByClassName('tab-button')[0].fireEvent(event)
is the way you could do it... but trying it on the site, the 'click' event isn't bound to that element
EDITED See How to trigger event in JavaScript?
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