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?

João Farias
  • 10,876
  • 2
  • 18
  • 39
QuelKitz
  • 21
  • 5

3 Answers3

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

PDHide
  • 11,065
  • 2
  • 14
  • 42
  • I used this code on tryit editor

    code: document.querySelector('a[href*="/p/"]:nth-child(4)').style.border = "10px

    TryIt Link: https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_document_queryselector_attribute

    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 '21 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 '21 at 13:38
  • This is the link

    Link:https://www.instagram.com/explore/locations/110148382341970

    – QuelKitz Mar 22 '21 at 14:50
  • @QuelKitz one example .vY_QD>div>div>div>div:nth-child(3)>div:nth-child(1) a – PDHide Mar 22 '21 at 15:06
  • It changes though its not always vY_QD – QuelKitz Mar 22 '21 at 16:11
  • @QuelKitz try this then await executeScript("document.evaluate('(//a[contains(@href,"/p/")])[10]', document,null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue").click() – PDHide Mar 22 '21 at 16:22
  • That clicks the first picture still for some reason – QuelKitz Mar 22 '21 at 16:28
  • it works fine what extension are you using ? – PDHide Mar 22 '21 at 16:38
  • open browser console and try runnign just run document.evaluate('(//a[contains(@href,"/p/")])[2]', document,null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click you can see that it clicks second image – PDHide Mar 22 '21 at 16:43
  • I did it in the console it didnt click anything for me – QuelKitz Mar 22 '21 at 17:28
  • Nevermind it worked in console 1 min – QuelKitz Mar 22 '21 at 17:30
  • sorry add click() at the end not click , document.evaluate('(//a[contains(@href,"/p/")])[2]', document,null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click() – PDHide Mar 22 '21 at 17:30
  • Yea I added that it works in console but when I added it to the google chrome extension it doesnt work it tried addin .click()"); .click()"; etc etc – QuelKitz Mar 22 '21 at 17:59
  • Which extension are you using – PDHide Mar 22 '21 at 18:36
  • DoneLikes Link:https://chrome.google.com/webstore/detail/donelikes-automatically-l/kpalbeafjagcohofnegkemfcbpkafhec?hl=en Im modifying it to my liking I cant get it to work though its in background.js – QuelKitz Mar 22 '21 at 18:41
  • how do you do that please explain the steps – PDHide Mar 22 '21 at 18:48
  • Download CRX Extractor Link:https://chrome.google.com/webstore/detail/crx-extractordownloader/ajkhmmldknmfjnmeedkbkkojgobmljda?hl=en-US Then go to DoneLikes Link:https://chrome.google.com/webstore/detail/donelikes-automatically-l/kpalbeafjagcohofnegkemfcbpkafhec?hl=en and right click in a white area and click Download CRX for this extension and click Download ZIP for this extension then unzip it and to load it you have to go to google chrome developer tools and load unpacked extension and load the folder you unzipped – QuelKitz Mar 22 '21 at 18:53
  • @QuelKitz see the updated answer you should use await executeScript("document.evaluate('(//a[contains(@href,\"/p/\")])[2]', document,null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click()"); replace line 183 with this content – PDHide Mar 22 '21 at 19:11
  • see full step in the update – PDHide Mar 22 '21 at 19:11
  • It didnt work it gets hung what are you using to edit the .js file? Im usin notepad – QuelKitz Mar 22 '21 at 19:23
  • I am using notepad++ it works really well – PDHide Mar 22 '21 at 19:26
  • Yea I have that too I had downloaded it for another purpose a while ago I just wasnt usin it lol but yea I got it to work – QuelKitz Mar 22 '21 at 19:32
  • Please upvote and click the tick sign if the answer solved the issue – PDHide Mar 22 '21 at 19:34
  • I did I dont kno if its the right one or not though cause other people answered too let me kno if I did the right one – QuelKitz Mar 22 '21 at 19:47
  • Yup it worked thanks :) – PDHide Mar 22 '21 at 19:58
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

Michael Durrant
  • 25,210
  • 3
  • 40
  • 112
  • Could you give me a example by usin this code? Code : await executeScript("document.querySelector('a[href*="/p/"]').click()"); – QuelKitz Mar 22 '21 at 00: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 }

Mark Mayo
  • 1,223
  • 12
  • 40