1

I want to click a link on a page but they all have similar hrefs

This works for clicking the first link

Code: await executeScript("document.querySelector('a[href*="/p/"]').click()");

How do I click the 10th link?

1

in css you have method called nth-child and nth-of-type and nth-child , use that to find the element

so assuming the href is inside an unique or the span containing the 'a' tag is inside a specific parent use something like

await executeScript("document.querySelector('div#parentid>span:nth-child(10) a[href*="/p/"]').click()"); 

This will find the div tag with id parentid , and finds the second direct span tag child , and finds the "a" tag with mentioned href any where under the span tag

or use xpath :

 await executeScript("document.evaluate('(//a[contains(@href,"/p/")])[10]', document,null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue").click()

Update:

As you are using chrome extension use below steps

Install:

https://chrome.google.com/webstore/detail/crx-extractordownloader/ajkhmmldknmfjnmeedkbkkojgobmljda?hl=en-US

Goto

https://chrome.google.com/webstore/detail/donelikes-automatically-l/kpalbeafjagcohofnegkemfcbpkafhec?hl=en

Download zip

Right click on the above page and click download as zip

update code

Update background.jslikeimage(url)" method in line 183**

await executeScript("document.evaluate('(//a[contains(@href,\"/p/\")])[2]', document,null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click()");

just replace with above content

install this modified extension

Goto: chrome://extensions/

and click Load unpacked extension and select the folder where you unzipped the content of the zip file

25
  • I used this code on tryit editor code: document.querySelector('a[href*=\"/p/\"]:nth-child(4)').style.border = "10px TryIt Link: w3schools.com/jsref/… It worked but it doesnt work on instagram cause the href is in a different div and the div changes so what should I do? – QuelKitz Mar 22 at 13:36
  • find the parent of that div which has that div, go up the tree until you find something unique. which element are you trying to find please add a screen shot and link – PDHide Mar 22 at 13:38
  • This is the link Link:instagram.com/explore/locations/110148382341970 – QuelKitz Mar 22 at 14:50
  • @QuelKitz one example .vY_QD>div>div>div>div:nth-child(3)>div:nth-child(1) a – PDHide Mar 22 at 15:06
  • It changes though its not always vY_QD – QuelKitz Mar 22 at 16:11
0

Select them all with document.querySelectorAll which will give you an array and then take the [9]th element

https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll

1
  • Could you give me a example by usin this code? Code : await executeScript("document.querySelector('a[href*="/p/"]').click()"); – QuelKitz Mar 22 at 0:42
0

On the assumption they're all ahrefs with different text, you could look for that - eg find the element with the text of the 10th link, and look for that.

Alternatively, you can access in the dom using querySelectorAll(), and then index the 10th one, by converting the NodeList returned to an array:

eg:

const nodesArray = [].slice.call(document.querySelectorAll(('a[href*="/p/"]')));

then use nodesArray[x] for the xth element.

Or just iteratate through the returned list:

var ps = querySelectorAll(('a[href*="/p/"]')), i;

for (i = 0; i < ps.length; ++i) {
  ps[i]...... //whatever you want to do with it
}
1
  • Im not sure how to implement this Im doing this to a js file for a chrome extension – QuelKitz Mar 22 at 0:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.