-2

enter image description hereI want to click the button on one website. My selectors highlight the button in google chrome console so it must be proper. When I send click() it returns undefined. What might be the possible reason of this happening ? My code is short and I put it directly to the chrome console:

document.querySelector("div#content table#content_table tbody tr td div#content_middle div div:nth-child(3) div button").click();
Pytong
  • 29
  • 4
  • We can't possible help you without seeing the HTML that you are trying to query. – Scott Marcus Dec 07 '19 at 17:26
  • Does this answer your question? [How to simulate a click with JavaScript?](https://stackoverflow.com/questions/2705583/how-to-simulate-a-click-with-javascript) – Kumar Swapnil Dec 07 '19 at 17:26
  • Show more codes! – Vahid Alimohamadi Dec 07 '19 at 17:27
  • Just added screenshoot of button html. My code is just the one line above what should be enough to click it. @KumarSwapnil it returns TRUE but doesn;t work. – Pytong Dec 07 '19 at 17:33
  • 2
    `HTMLElement.click()` has no return value so of course you have `undefined`... – Niet the Dark Absol Dec 07 '19 at 17:35
  • @NiettheDarkAbsol I can click all other button in the way presented above, just this one doesn't work. – Pytong Dec 07 '19 at 17:36
  • Simpy pick the `id` and try instead of `class`. Then you can figure out something. – Kumar Swapnil Dec 07 '19 at 17:40
  • @KumarSwapnil Id is dynamic and I also tried to use it. I wrote simple code to catch the new id each time but the final efect is the same :( – Pytong Dec 07 '19 at 17:42
  • Please DON'T show pictures of code, show the actual code as text in your question. Also, you need to show enough of the code so that we can help. You have a crazy long selector, so we need to see that entire structure. – Scott Marcus Dec 07 '19 at 17:57
  • **Any** Javascript function that has no explicit return value returns `undefined`. That's just standard behaviour. – connexo Dec 07 '19 at 18:02

1 Answers1

0

This works:

<button onclick="alert('button was clicked.');">click me</button>
<script>
    document.querySelector("button").click();
</script>

Do you have an onclick function bound to the element?

Aaron Plocharczyk
  • 2,776
  • 2
  • 7
  • 15
  • Probably I did not ask the question properly. When I run my code i want browser to click this button. It does not click on it. Similar code in selenium do the job but I need to figure it out in js. – Pytong Dec 07 '19 at 17:45
  • does document.querySelector("div#content table#content_table tbody tr td div#content_middle div div:nth-child(3) div button") return null? – Aaron Plocharczyk Dec 07 '19 at 17:47
  • It does not. As mentioned, google chrome console cleary says this element exists and moreover highlights it. – Pytong Dec 07 '19 at 17:51
  • Then it should be clicking it. Are you sure you have an onclick function bound to the element? Check my updated answer for a working example. – Aaron Plocharczyk Dec 07 '19 at 17:55
  • Onclick says it's been clicked. However it does not work as it should. When I click it manually, it navigates me to another page. – Pytong Dec 07 '19 at 17:59
  • Would you mind adding the onclick function to your question so we can review that then? – Aaron Plocharczyk Dec 07 '19 at 18:00
  • 1
    Well I've resigned from js and sticked to working selenium. Thanks anyway :) – Pytong Dec 08 '19 at 07:31