0

my current work is basically clicking a button every couple of minutes and check if there are any new tickets.

With some programming background I thought this would be an easy-to-automate task, but being completely new to javascript, I'm already struggling with the "button clicking".

Unfortunately, because it is an internal website, I can't share the exact code, but I'll try to give as much information as I am able to.

I tried to "click" in the console:

'document.getElementById("IDofButton").click()'

But it gives me an error "undefined"; without the ".click()" at the end I get the correct element as a result. I tried it with "$" instead of document.getElementById, but even that didn't work, so I gave up on the idea of simply clicking the button, even though I think that's the easier way.

Debugging in chrome with breakpoints and the performance profiler, I could find the first called function "h()", it runs for 0.020 ms, then another function gets called, running for 0.012 ms and then a longer function gets called running for 14.8 ms, several others follow. My guess or better hope, was that the first two functions check something and then start the whole process. But I can't simply call any of the functions with like the following example syntax: "h()".

I'd be very glad, if anyone has an idea or could point me in the right direction. Thank you very much.

EDIT/UPDATE:

I finally found the problem: While debugging with the performance profiler I saw, that the functions are triggered by a mouseup event and I thought the "[...].click()" simulates a mousedown and a mouseup instead of a click. I now copied a function simulating the whole mouseover>mousedown>mouseup>click and it works as expected.

Thank you to all who helped me with the whole situation!

Chinx7
  • 13
  • 1
  • 4
  • 1
    Does this answer your question? [Click a button programmatically - JS](https://stackoverflow.com/questions/24278469/click-a-button-programmatically-js) – evolutionxbox Jun 29 '20 at 16:20
  • 1
    If not, your JS might be loaded/running before the DOM is ready. https://stackoverflow.com/questions/43947523/document-queryselector-always-returns-null – evolutionxbox Jun 29 '20 at 16:20
  • @evolutionxbox, that helps a lot, but I think my problem right now is that I can't use "[...].click()", why? I am still trying to figure out. And I think using the console after the site is loaded should eliminate the problem of my JS running before the DOM is ready? – Chinx7 Jun 29 '20 at 17:22

2 Answers2

0

setInterval(

function(){

document.getElementById('IDofButton').click()

},5000)// time in milliseconds i set for 5 sec

Above time set for each 5 Seconds.

if doesn't work Let me know.

anynomus
  • 62
  • 2
  • Hello, thank you for the quick answer, I think I'll try your solution for the automation part, but my main problem right now is that I can't attach the ".click()" at the end, it always gives me an "undefined" as a result. – Chinx7 Jun 29 '20 at 17:18
  • right click on the element that you want to attach and select inspect elements and set some id for it then try that method which i send you it will work defiantly remember set id only which you have been set for that specific element. further help send me screen shorts i will tell u what to do ! – anynomus Jun 29 '20 at 17:22
  • I updated my post with the solution, thank you for the Interval function, I will most likely use it. – Chinx7 Jun 29 '20 at 18:17
0

You get undefined because your selector is wrong. Try learning css selectors and see if it helps you(https://www.w3schools.com/cssref/css_selectors.asp)

You can also inspect the elements in the dev tools and right clicking on the element, going to copy->Copy JS Path. This will give you the correct selector.

Also you can select the element and reference it in the dev tools console with $0.

Then if you want to re-click every second then use setInterval

setInterval(() => {
    document.querySelector('#someId').click()
}, 1000)

Hope that helps you, good luck!

Ilan Khirin
  • 351
  • 1
  • 4
  • Hello, thank you for the quick answer, using "Copy JS Path" is a nice, quicker way to get the element, I'll remember that. Just putting that into the console gives me the same result as "doc...byID". And when I attach the ".click()" it again says undefined. – Chinx7 Jun 29 '20 at 17:16
  • 1
    It's ok that you get undefined, it doesn't mean that there is an error, it just that click() doesn't return a value. The reason nothing happens is because the element you are selecting doesn't handle the click event. Try playing around with the children of that element or the parents. You can use the "Copy JS Path" to quickly test it – Ilan Khirin Jun 29 '20 at 17:21
  • Using the "Copy JS Path" I tried it on some different websites and it worked everywhere I tested. On this internal website however, I can't click any button, I tried every child and every parent of different buttons, but no luck. I think I have to figure out, why I can't click on this specific website. – Chinx7 Jun 29 '20 at 17:45
  • I wish you could show me the site but you can try using Selenium or Cypress – Ilan Khirin Jun 29 '20 at 17:49
  • 1
    Thank you for the help, I figured out what my actual problem was, as you can see in the edit. I won't forget the "Copy JS Path", that thing is a real help, thank you for that! – Chinx7 Jun 29 '20 at 18:16
  • That's weird but I'm glad you figured it out, good job! – Ilan Khirin Jun 29 '20 at 18:39