admin 管理员组文章数量: 1086019
I am trying to make a Javascript code that automates a button click on a webpage, so I am trying to figure out the code with Google Chrome's Console. This is the button:
<a href="#" class="link">Get Link</a>
I thought I could simply write this:
var button = document.getElementsByClassName('link');
button.click()
But this message appears:
"Uncaught TypeError: button.click is not a function at <anonymous>:2:8"
Any solution? Thanks for the help.
I am trying to make a Javascript code that automates a button click on a webpage, so I am trying to figure out the code with Google Chrome's Console. This is the button:
<a href="#" class="link">Get Link</a>
I thought I could simply write this:
var button = document.getElementsByClassName('link');
button.click()
But this message appears:
"Uncaught TypeError: button.click is not a function at <anonymous>:2:8"
Any solution? Thanks for the help.
Share Improve this question asked Jun 3, 2019 at 15:03 Javier CalvoJavier Calvo 391 gold badge1 silver badge4 bronze badges2 Answers
Reset to default 6getElementsByClassName
returns a live HTMLCollection
, not a single element.
elements is a live
HTMLCollection
of found elements.
So if you want to use getElementsByClassName
, you need to get the first item from the iterable like this:
var button = document.getElementsByClassName('link');
button[0].click()
If you want to get a single element, use document.querySelector()
. This will return the first found element.
var button = document.querySelector('.link');
button.click()
This is a screenshot of the line I wrote
Is it correct?
本文标签: How to perform a click() using Google Chrome39s Console (Javascript)Stack Overflow
版权声明:本文标题:How to perform a click() using Google Chrome's Console? (Javascript) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744100214a2533462.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论