admin 管理员组

文章数量: 1086019

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();
}

本文标签: google chromeJavascript console click links with certain titletextStack Overflow