Skip to content

Commit 2a3c1c1

Browse files
committed
support child elements
1 parent 11cf85d commit 2a3c1c1

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

phantomime/phantomime.py

+23-10
Original file line numberDiff line numberDiff line change
@@ -311,51 +311,63 @@ def wait_text_on_page(text: str, timeout: int = 30):
311311

312312
@decorators._must_have_supported_selector_type
313313
@decorators._must_have_driver_initialized
314-
def find_element(selector_type: str, selector: str) -> WebElement:
314+
def find_element(selector_type: str, selector: str, parent_el: WebElement = None) -> WebElement:
315315
"""
316316
Find the first element matching the given selector and selector type.
317+
If parent_el is set, it will search for a child element.
317318
"""
318319
_log.debug(
319320
f"finding element matching {selector} by selector type {selector_type}")
320321

321322
try:
322-
return _driver.find_element(getattr(By, selector_type), selector)
323+
root_el = _driver
324+
if parent_el is not None:
325+
root_el = parent_el
326+
327+
return root_el.find_element(getattr(By, selector_type), selector)
323328
except:
324329
return None
325330

326331

327332
@decorators._must_have_supported_selector_type
328333
@decorators._must_have_driver_initialized
329-
def find_select_element(selector_type: str, selector: str) -> Select:
334+
def find_select_element(selector_type: str, selector: str, parent_el: WebElement = None) -> Select:
330335
"""
331336
Find the first element matching the given selector and selector type and return it as a Select wrapped WebElement.
337+
If parent_el is set, it will search for a child element.
332338
"""
333339
_log.debug(
334340
f"finding element matching {selector} by selector type {selector_type} and returning it as a Select wrapped WebElement")
335341

336-
return Select(find_element(selector_type, selector))
342+
return Select(find_element(selector_type, selector, parent_el))
337343

338344

339345
@decorators._must_have_supported_selector_type
340346
@decorators._must_have_driver_initialized
341-
def find_elements(selector_type: str, selector: str) -> List[WebElement]:
347+
def find_elements(selector_type: str, selector: str, parent_el: WebElement = None) -> List[WebElement]:
342348
"""
343349
Find all elements matching the given selector and selector type.
350+
If parent_el is set, it will search for child elements.
344351
"""
345352
_log.debug(
346353
f"finding elements matching {selector} by selector type {selector_type}")
347354

348355
try:
349-
return _driver.find_elements(getattr(By, selector_type), selector)
356+
root_el = _driver
357+
if parent_el is not None:
358+
root_el = parent_el
359+
360+
return root_el.find_elements(getattr(By, selector_type), selector)
350361
except:
351362
return None
352363

353364

354365
@decorators._must_have_supported_selector_type
355366
@decorators._must_have_driver_initialized
356-
def wait_element_exists(selector_type: str, selector: str, timeout: int = 10) -> WebElement:
367+
def wait_element_exists(selector_type: str, selector: str, timeout: int = 10, parent_el: WebElement = None) -> WebElement:
357368
"""
358369
Waits for an element matching the given selector by selector_type to exist on the current page.
370+
If parent_el is set, it will wait for a child element.
359371
"""
360372
_log.debug(
361373
f"waiting for element matching {selector} by selector type {selector_type} to exist")
@@ -367,14 +379,15 @@ def wait_element_exists(selector_type: str, selector: str, timeout: int = 10) ->
367379
max_time=timeout
368380
)
369381

370-
return b(find_element)(selector_type, selector)
382+
return b(find_element)(selector_type, selector, parent_el)
371383

372384

373385
@decorators._must_have_supported_selector_type
374386
@decorators._must_have_driver_initialized
375-
def wait_element_not_exists(selector_type: str, selector: str, timeout: int = 10):
387+
def wait_element_not_exists(selector_type: str, selector: str, timeout: int = 10, parent_el: WebElement = None):
376388
"""
377389
Waits for an element matching the given selector by selector_type to not exist on the current page.
390+
If parent_el is set, it will wait for a child element.
378391
"""
379392
_log.debug(
380393
f"waiting for element matching {selector} by selector type {selector_type} to not exist")
@@ -386,7 +399,7 @@ def wait_element_not_exists(selector_type: str, selector: str, timeout: int = 10
386399
max_time=timeout
387400
)
388401

389-
b(find_element)(selector_type, selector)
402+
b(find_element)(selector_type, selector, parent_el)
390403

391404

392405
@decorators._must_have_driver_initialized

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name='phantomime',
5-
version='v2.6.0-alpha',
5+
version='v2.7.0-beta',
66
packages=find_packages(),
77
url='https://github.com/psyb0t/phantomime',
88
license='WTFPL',

0 commit comments

Comments
 (0)