hide-if-matches-computed-xpath

hide-if-matches-computed-xpath

The hide-if-matches-computed-xpath snippet hides specific elements whose class name or ID changes at runtime through a dynamically-built XPath query. It takes an incomplete XPath selector with a {{}} placeholder and fills it using searchQuery and searchRegex.

Always use double quotes instead of single quotes inside selectors.

Parameters

Name

Description

Mandatory

query

Template XPath query. Use {{}} as placeholder for dynamic insertion.

Yes

searchQuery

XPath query to search for an element used alongside searchRegex.

Yes

searchRegex

Regular expression to extract text from the innerHTML of the element matching searchQuery.

Yes

waitUntil

Delay until given state (loading, interactive, complete, load, or event name).

No

Filter examples

Filter

Result

hide-if-matches-computed-xpath '//[@class="{{}}"]' '//div[@id="target"]' '/./'

Hides all elements with a class matching any string in the target div.

hide-if-matches-computed-xpath '//[@class="{{}}"]/child::text()[contains(.,"Sponsored")]' '//div[@id="target"]' '/./'

Hides elements containing "Sponsored" with a dynamically computed class.

Other resources