Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a method to retrieve all elements that comply with xpath #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions hmdriver2/_xpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@


class _XPath:
def __init__(self, d: Driver):
def __init__(self, d: Driver,first_event=True):
self._d = d
self.first_event=first_event

def __call__(self, xpath: str) -> '_XMLElement':

Expand All @@ -25,11 +26,21 @@ def __call__(self, xpath: str) -> '_XMLElement':
result = xml.xpath(xpath)

if len(result) > 0:
node = result[0]
raw_bounds: str = node.attrib.get("bounds") # [832,1282][1125,1412]
bounds: Bounds = parse_bounds(raw_bounds)
logger.debug(f"{xpath} Bounds: {bounds}")
return _XMLElement(bounds, self._d)
if self.first_event:
node = result[0]
raw_bounds: str = node.attrib.get("bounds") # [832,1282][1125,1412]
bounds: Bounds = parse_bounds(raw_bounds)
logger.debug(f"{xpath} Bounds: {bounds}")
return _XMLElement(bounds, self._d)
else:
result_event_list = []
for node in result:
raw_bounds: str = node.attrib.get("bounds") # [832,1282][1125,1412]
bounds: Bounds = parse_bounds(raw_bounds)
logger.debug(f"{xpath} Bounds: {bounds}")
result_event_list.append(_XMLElement2(bounds, self._d))
return result_event_list


return _XMLElement(None, self._d)

Expand Down
9 changes: 9 additions & 0 deletions hmdriver2/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,12 @@ def xpath(self):
"""
from ._xpath import _XPath
return _XPath(self)

@cached_property
def xpath_all_event(self):
"""
d.xpath_all_event("//*[@text='Hello']")
:return: [event,event]
"""
from ._xpath import _XPath
return _XPath(self,first_event=False)