最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

google chrome - Javascript console: click links with certain titletext - Stack Overflow

programmeradmin3浏览0评论

I'm trying to click all the links on a page, where the identifiers for the link I want to click are only a unique href, a generic title, and the text of the link.

I want to click all the links whose title text (or even the text of the link) are the same.

I've tried adapting other scripts for the purpose, but have had no luck. My current iteration is this:

var links = document.getElementByTitle("Download");
for (var i = 0; i < links.length; ++i) {
links[i].click();
}

which doesn't work because there is no method getElementByTitle. I feel like this is the best way to convey what I'm trying to do, though.

I'm trying to click all the links on a page, where the identifiers for the link I want to click are only a unique href, a generic title, and the text of the link.

I want to click all the links whose title text (or even the text of the link) are the same.

I've tried adapting other scripts for the purpose, but have had no luck. My current iteration is this:

var links = document.getElementByTitle("Download");
for (var i = 0; i < links.length; ++i) {
links[i].click();
}

which doesn't work because there is no method getElementByTitle. I feel like this is the best way to convey what I'm trying to do, though.

Share Improve this question asked Mar 7, 2014 at 23:14 AndrewAndrew 992 silver badges12 bronze badges 1
  • 1 "I want to click all the links whose title text (or even the text of the link) are the same." - The same as what? Are you saying you want to find any links that have the same title as other links and click all of those? Or where the title is the same as some predefined string? – nnnnnn Commented Mar 7, 2014 at 23:20
Add a ment  | 

1 Answer 1

Reset to default 7

"there is no method getElementByTitle."

No, but you can use the querySelectorAll() method:

var links = document.querySelectorAll('a[title="Download"]');
for (var i = 0; i < links.length; i++) {
    links[i].click();
}

That is supported in all modern browsers, including IE >= 9, or even IE8 with CSS2 selectors. Otherwise you can do this:

var links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
    if (links[i].title === "Download")
        links[i].click();
}
发布评论

评论列表(0)

  1. 暂无评论