symfony / panther

A browser testing and web crawling library for PHP and Symfony
MIT License
2.94k stars 222 forks source link

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

Open monkeyArms opened 3 years ago

monkeyArms commented 3 years ago

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

dunglas commented 3 years ago

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

monkeyArms commented 3 years ago

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], [ '/', '(' ] )
dunglas commented 3 years ago

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