Chrome 元素控制台在给定 xPath 时不显示唯一元素

Chrome Elements console does not show unique element when given xPath

如果我按 F12,转到 Elements 并单击 ctrl+F 搜索 xPath,如果我只找到一个元素,则该元素不会突出显示(但 Chrome 会指出 1 of 1)。如果我找到多个元素,Chrome 从第二个元素中突出显示。当我在 xPath 中使用索引时也会出现问题:(//a)[1]

似乎 Chrome return 每个元素的属性都包含 url 的一部分(meta@contentscript @src,...)。这就是当您输入 //a 时得到 script 元素的原因。然而,(//a)[1] 应该工作并且应该 return 第一个锚点(使用 Edge ChromiumChrome 测试良好)。

尝试将此解决方法用于 select 元素 :

//a[self::a]

要获取页面上的第一个锚点,请使用:

(//a[self::a])[1]

参考图片:

您似乎在谈论 Chromium 问题论坛中报告的这个错误 https://bugs.chromium.org/p/chromium/issues/detail?id=1106703

它是在 Chrome 84 中通过此更改引入的 https://developers.google.com/web/updates/2020/05/devtools

我想我们只能等待或降级到 Chrome 83(这在公司环境中可能是不可能的)

根本原因是DevTools within Google Chrome 84.0没有突出显示第一个匹配元素。

如果定位器找到一个匹配项,搜索结果会显示 1 of 1,但 isn't getting highlighted within the HTML DOM

作为演示,Google Home Page can be identified uniquely using the :

中的 搜索框
[name='q']

或使用 :

//*[@name='q']

在 Google Chrome 84.0 中,确实找到了元素并显示 1 of 1 但元素没有突出显示。

Incase,有多个元素匹配到 ,除了第一个匹配的元素,其他元素确实被突出显示。


Chrome84

中的错误

此问题是在 Platform>DevTools 队列中通过 Issue 1108311: The first matched element in the Elements panel is not getting highlighted as per the cssSelector and had been merged into Issue 1103316: Elements search does not resolveNode (highlight text, etc) on first search result 提出的,我们正在积极跟踪该问题。


解决方案

根据 @bugdroid,主要问题的产生是因为确保搜索结果有效的检查没有考虑索引 0 的情况,因此索引的所有突出显示结果 0(用户索引 1)不再突出显示。

此问题的 fix Merge-Approved 在:


替代解决方案

对于使用当前 Version 84.0.4147.89 you can find a detailed discussion in Why XPath does not highlighted the yellow mark in Chrome84?

的替代解决方案