Skip to content

Commit e1e9f3a

Browse files
authored
Merge pull request #267 from Cvoluj/main
fix(elements): correctly detect parenthesized XPath expressions
2 parents 94277d7 + f05e39d commit e1e9f3a

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

pydoll/elements/mixins/find_elements_mixin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ def _get_expression_type(expression: str) -> By:
478478
- XPath: starts with ./, or /
479479
- Default: CSS_SELECTOR
480480
"""
481-
if expression.startswith('./') or expression.startswith('/'):
481+
if expression.startswith(('./', '/', '(/')):
482482
return By.XPATH
483483

484484
return By.CSS_SELECTOR

tests/test_find_elements_mixin.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,16 @@ def test_edge_case_expressions(self):
201201
# Empty string should default to CSS
202202
assert FindElementsMixin._get_expression_type('') == By.CSS_SELECTOR
203203

204+
def test_xpath_with_parentheses_and_predicate(self):
205+
"""Test XPath detection with parentheses, e.g. (//div)[last()]."""
206+
expressions = [
207+
'(//div)[last()]',
208+
'(//span[@class="btn"])[1]',
209+
'(/html/body/div)[position()=1]'
210+
]
211+
for expr in expressions:
212+
assert FindElementsMixin._get_expression_type(expr) == By.XPATH
213+
204214

205215
class TestEnsureRelativeXPath:
206216
"""Test the _ensure_relative_xpath static method."""

0 commit comments

Comments
 (0)