I.sh.
I.sh.

Reputation: 2072

Xpath - locate all tags starts-with

I'm using Playwright and I have the following HTML code:

<body>
  <custom-element-a>some text</custom-element-a>
  <custom-element-b>some text</custom-element-b>
  <custom-element-foo>some text</custom-element-foo>
  <custom-element-bar>some text</custom-element-bar>
</body>

And I want to write an Xpath expression that returns all of the elements that start-with custom-element sub-string.

What I've tried:

//*[starts-with(name(), 'custom-element')]

//custom-element*

//self::custom-element*

//*[(self::custom-element*)]

Couldn't find any solution.

Upvotes: 2

Views: 55

Answers (1)

I.sh.
I.sh.

Reputation: 2072

It took me some time, but I've found a solution - by combining the local-name function and starts-with function:

//*[starts-with(local-name(), 'custom-element')]

According to MDN docs:

The local-name function returns a string representing the local name of the first node in a given node-set.

local-name( [node-set] )

Thanks for @MadsHansen for mentioning that it his comment.

Upvotes: -3

Related Questions