Update CDP Mode

This commit is contained in:
Michael Mintz 2025-02-24 16:05:05 -05:00
parent 1f581ed757
commit 1019b488f8
1 changed files with 20 additions and 10 deletions

View File

@ -288,7 +288,7 @@ class CDPMethods():
return updated_elements return updated_elements
def select(self, selector, timeout=None): def select(self, selector, timeout=None):
"""Similar to find_element(), but without text-based search.""" """Similar to find_element()."""
if not timeout: if not timeout:
timeout = settings.SMALL_TIMEOUT timeout = settings.SMALL_TIMEOUT
self.__add_light_pause() self.__add_light_pause()
@ -297,12 +297,25 @@ class CDPMethods():
tag_name = selector.split(":contains(")[0].split(" ")[-1] tag_name = selector.split(":contains(")[0].split(" ")[-1]
text = selector.split(":contains(")[1].split(")")[0][1:-1] text = selector.split(":contains(")[1].split(")")[0][1:-1]
with suppress(Exception): with suppress(Exception):
new_timeout = timeout
if new_timeout < 1:
new_timeout = 1
self.loop.run_until_complete( self.loop.run_until_complete(
self.page.select(tag_name, timeout=5) self.page.select(tag_name, timeout=new_timeout)
) )
self.loop.run_until_complete(self.page.find(text, timeout=5)) self.loop.run_until_complete(
element = self.find_elements_by_text(text, tag_name=tag_name)[0] self.page.find(text, timeout=new_timeout)
return self.__add_sync_methods(element) )
elements = self.find_elements_by_text(text, tag_name=tag_name)
if not elements:
plural = "s"
if timeout == 1:
plural = ""
msg = "\n Element {%s} was not found after %s second%s!"
message = msg % (selector, timeout, plural)
raise Exception(message)
element = self.__add_sync_methods(elements[0])
return element
failure = False failure = False
try: try:
element = self.loop.run_until_complete( element = self.loop.run_until_complete(
@ -313,11 +326,8 @@ class CDPMethods():
plural = "s" plural = "s"
if timeout == 1: if timeout == 1:
plural = "" plural = ""
message = "\n Element {%s} was not found after %s second%s!" % ( msg = "\n Element {%s} was not found after %s second%s!"
selector, message = msg % (selector, timeout, plural)
timeout,
plural,
)
if failure: if failure:
raise Exception(message) raise Exception(message)
element = self.__add_sync_methods(element) element = self.__add_sync_methods(element)