Skip to content

Commit 85609e9

Browse files
authored
Merge pull request #187 from radiantly/fix-query
fix: use correct class name and id selector in query()
2 parents 302707b + 5a066d6 commit 85609e9

File tree

2 files changed

+8
-30
lines changed

2 files changed

+8
-30
lines changed

pydoll/elements/mixins/find_elements_mixin.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import asyncio
2-
import re
32
from typing import TYPE_CHECKING, Optional, TypeVar, Union
43

54
from pydoll.commands import (
@@ -352,18 +351,11 @@ def _get_expression_type(expression: str) -> By:
352351
Auto-detect selector type from expression syntax.
353352
354353
Patterns:
355-
- XPath: starts with //, .// , ./, or /
356-
- ID: starts with #
357-
- Class: starts with . (but not ./)
354+
- XPath: starts with ./, or /
358355
- Default: CSS_SELECTOR
359356
"""
360-
xpath_pattern = r'^(//|\.//|\.\/|/)'
361-
if re.match(xpath_pattern, expression):
357+
if expression.startswith('./') or expression.startswith('/'):
362358
return By.XPATH
363-
if expression.startswith('#'):
364-
return By.ID
365-
if expression.startswith('.') and not expression.startswith('./'):
366-
return By.CLASS_NAME
367359

368360
return By.CSS_SELECTOR
369361

@@ -435,7 +427,7 @@ def _get_find_elements_command(self, by: By, value: str, object_id: str = ''):
435427
case _:
436428
selector = escaped_value
437429
if object_id and not by == By.XPATH:
438-
script = Scripts.RELATIVE_QUERY_SELECTOR_ALL.replace('{selector}', escaped_value)
430+
script = Scripts.RELATIVE_QUERY_SELECTOR_ALL.replace('{selector}', selector)
439431
command = RuntimeCommands.call_function_on(
440432
function_declaration=script,
441433
object_id=object_id,

tests/test_find_elements_mixin.py

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -168,19 +168,6 @@ def test_xpath_single_slash(self):
168168
"""Test XPath detection with single slash."""
169169
assert FindElementsMixin._get_expression_type('/html/body') == By.XPATH
170170

171-
def test_id_selector(self):
172-
"""Test ID selector detection."""
173-
assert FindElementsMixin._get_expression_type('#main-content') == By.ID
174-
175-
def test_class_selector(self):
176-
"""Test class selector detection."""
177-
assert FindElementsMixin._get_expression_type('.btn-primary') == By.CLASS_NAME
178-
179-
def test_class_selector_not_xpath(self):
180-
"""Test that class selector doesn't conflict with XPath dot slash."""
181-
assert FindElementsMixin._get_expression_type('.button') == By.CLASS_NAME
182-
assert FindElementsMixin._get_expression_type('./button') == By.XPATH
183-
184171
def test_css_selector_default(self):
185172
"""Test CSS selector as default."""
186173
assert FindElementsMixin._get_expression_type('div.content > p') == By.CSS_SELECTOR
@@ -192,6 +179,11 @@ def test_css_selector_attribute(self):
192179
def test_css_selector_pseudo_class(self):
193180
"""Test CSS selector with pseudo-classes."""
194181
assert FindElementsMixin._get_expression_type('button:hover') == By.CSS_SELECTOR
182+
183+
def test_css_selector_not_xpath(self):
184+
"""Test that css selector doesn't conflict with XPath dot slash."""
185+
assert FindElementsMixin._get_expression_type('.button') == By.CSS_SELECTOR
186+
assert FindElementsMixin._get_expression_type('./button') == By.XPATH
195187

196188
def test_complex_xpath_expressions(self):
197189
"""Test complex XPath expressions are detected correctly."""
@@ -208,12 +200,6 @@ def test_edge_case_expressions(self):
208200
"""Test edge case expressions."""
209201
# Empty string should default to CSS
210202
assert FindElementsMixin._get_expression_type('') == By.CSS_SELECTOR
211-
212-
# Just a dot should be class selector
213-
assert FindElementsMixin._get_expression_type('.') == By.CLASS_NAME
214-
215-
# Just a hash should be ID selector
216-
assert FindElementsMixin._get_expression_type('#') == By.ID
217203

218204

219205
class TestEnsureRelativeXPath:

0 commit comments

Comments
 (0)