CCC
CCC

Reputation: 200

Chrome Elements console does not show unique element when given xPath

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]. enter image description here

enter image description here

enter image description here

Upvotes: 1

Views: 1443

Answers (3)

undetected Selenium
undetected Selenium

Reputation: 193238

The root cause is DevTools within Google Chrome 84.0 doesn't highlights the first matching element.

If, the locator finds a single match, the search result does show 1 of 1 but the WebElement isn't getting highlighted within the HTML DOM

As a demonstration, the Search Box within the Google Home Page can be identified uniquely using the :

[name='q']

or using the :

//*[@name='q']

within Google Chrome 84.0, does finds the element and shows 1 of 1 but the element is not getting highlighted.

devtools_issue

Incase, there are multiple element matching to the Locator Strategy, barring the first matched element, the other elements does gets highlighted.


Bug in Chrome 84

This issue was raised within Platform>DevTools queue through 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 where we are actively tracking the issue.


Solution

As per @bugdroid the main issue was caused because a check to ensure the search results were valid did not account for the case where the index was 0, so all highlight results of index 0 (index 1 to the user) were no longer highlighted.

The fix for this issue is Merge-Approved in:


Alternate Solution

For alternate solutions using the current Version 84.0.4147.89 you can find a detailed discussion in Why XPath does not highlighted the yellow mark in Chrome84?

Upvotes: 0

Oikuar
Oikuar

Reputation: 55

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)

Upvotes: 2

E.Wiest
E.Wiest

Reputation: 5915

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 :

A B C

Upvotes: 1

Related Questions