panther icon indicating copy to clipboard operation
panther copied to clipboard

XPath nth element selector is treated as CSS, and is invalid

Open monkeyArms opened this issue 4 years ago • 3 comments

A valid XPath selector for nth element, e.g. (//a)[3] is treated as a CSS selector because it does not start with a /, which causes an "invalid selector" exception to be thrown.

https://github.com/symfony/panther/blob/e53feac1df95f2022979e86f40b2540306581c3c/src/Client.php#L726

monkeyArms avatar Sep 28 '21 18:09 monkeyArms

Good catch. Maybe could we introduce a new parameter to force the selector to be interpreted as xpath?

dunglas avatar Sep 28 '21 18:09 dunglas

It might be nice to have that option. I would think simply changing that line to this would fix the issue as well since CSS selectors cannot start with an open parenthesis:

return empty( $locator ) || !in_array( $locator[0], [ '/', '(' ] )

monkeyArms avatar Sep 29 '21 20:09 monkeyArms

This works for me. Do you want to open a PR?

dunglas avatar Sep 29 '21 21:09 dunglas