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?
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?
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:
Goto
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
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
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
}
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:36Link:https://www.instagram.com/explore/locations/110148382341970
– QuelKitz Mar 22 '21 at 14:50.vY_QD>div>div>div>div:nth-child(3)>div:nth-child(1) a– PDHide Mar 22 '21 at 15:06await executeScript("document.evaluate('(//a[contains(@href,"/p/")])[10]', document,null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue").click()– PDHide Mar 22 '21 at 16:22document.evaluate('(//a[contains(@href,"/p/")])[2]', document,null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.clickyou can see that it clicks second image – PDHide Mar 22 '21 at 16:43document.evaluate('(//a[contains(@href,"/p/")])[2]', document,null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click()– PDHide Mar 22 '21 at 17:30await 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