问题
if I press F12
, go to Elements
and click ctrl+F
to search for a xPath, if I find only one element, that element is not highlighted (but Chrome will point out 1 of 1
). If i find more than one elements, Chrome highlights from the second one. Problem occures also when I use indexes in xPath: (//a)[1]
.
回答1:
Seems like you are talking about this bug https://bugs.chromium.org/p/chromium/issues/detail?id=1106703 which was reported in the Chromium issue forums.
It was introduced in Chrome 84 with this change https://developers.google.com/web/updates/2020/05/devtools
I guess we can only wait or downgrade to Chrome 83 (which might not be possible in a corporate environment)
回答2:
It seems Chrome returns every element with an attribute containing part of an url (meta
with @content
, script
with @src
,...). That's why you got a script
element when //a
is your input. However, (//a)[1]
should work and should return the first anchor (tested fine with Edge Chromium
and Chrome
).
Try to use this workaround to select the element :
//a[self::a]
To get the first anchor on the page, use :
(//a[self::a])[1]
Images for reference :
来源:https://stackoverflow.com/questions/62950893/chrome-elements-console-does-not-show-unique-element-when-given-xpath