I just found something very weird. Its about this css selector
div:nth-child(3) a
On X I have a container div selected in my DevTools
Now, with the css selector div:nth-child(3) a
I want to find the link holding the 4h
text
I copied the HTML from X to produce a demo here. You can see there that it works as expected, I see the correct href value!
Although this sounds super easy, which it is. Simplified the HTML looks like this:
<div>
<div><a href="/elonmusk">@elonmusk<a/></div>
<div>·</div>
<div>
<a href="/status/234324234234234">3h</div>
</div>
</div>
However, when I try to reproduce this on X.com it doesn’t work
To reproduce, first select the container element
Now if I perform that query I get
It selects the first div
. It can be fix when I query it as follows
$0.querySelector('div:nth-child(3)').querySelector('a')
Why does it work in my demo and not on X?
2
Answers
That’s because your selector doesn’t ask for the
<a>
to be a direct descendant and on X, your<a>
element is a descendant of another ancestor<div>
that’s also the third child of its parent.The same situation can be reproduced with the following tree:
You probably wanted
"div:nth-child(3) > a"
, which would match your element in X, but the best might be to use the:scope
pseudo-selector which will set the root of the selector to the element on which you called the query from:The HTML subtree you looked at is
Now
$0.querySelectorAll("div:nth-child(3) a")
selects alla
elements that$0
anddiv
that is the third among its siblings. This ancestor can be outside of the subtree you are looking at!The result of this query contains both
$3
(with$2
as the ancestor) and$1
(with an ancestor outside of this subtree).And
$0.querySelector("div:nth-child(3) a")
returns only the first of these, that is,$1
.What you want is correctly expressed as
$0.querySelector("div:nth-child(3)").querySelector("a")
.