Skip to content

Commit

Permalink
Merge pull request #3333 from seleniumbase/cdp-mode-patch-19
Browse files Browse the repository at this point in the history
CDP Mode - Patch 19
  • Loading branch information
mdmintz authored Dec 13, 2024
2 parents 992212c + 79fbbce commit 6e0c8c5
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 12 deletions.
1 change: 1 addition & 0 deletions examples/cdp_mode/ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ sb.cdp.add_handler(event, handler)
sb.cdp.find_element(selector)
sb.cdp.find(selector)
sb.cdp.locator(selector)
sb.cdp.find_element_by_text(text, tag_name=None)
sb.cdp.find_all(selector)
sb.cdp.find_elements_by_text(text, tag_name=None)
sb.cdp.select(selector)
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mycdp>=1.1.0
pynose>=1.5.3
platformdirs>=4.3.6
typing-extensions>=4.12.2
sbvirtualdisplay>=1.3.0
sbvirtualdisplay>=1.3.1
six>=1.17.0
parse>=1.20.2
parse-type>=0.6.4
Expand Down
2 changes: 1 addition & 1 deletion seleniumbase/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# seleniumbase package
__version__ = "4.33.8"
__version__ = "4.33.9"
1 change: 1 addition & 0 deletions seleniumbase/core/browser_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ def uc_open_with_cdp_mode(driver, url=None):
cdp.find_element = CDPM.find_element
cdp.find = CDPM.find_element
cdp.locator = CDPM.find_element
cdp.find_element_by_text = CDPM.find_element_by_text
cdp.find_all = CDPM.find_all
cdp.find_elements_by_text = CDPM.find_elements_by_text
cdp.select = CDPM.select
Expand Down
90 changes: 81 additions & 9 deletions seleniumbase/core/sb_cdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,57 @@ def find_element(
self.__slow_mode_pause_if_set()
return element

def find_element_by_text(
self, text, tag_name=None, timeout=settings.SMALL_TIMEOUT
):
"""Returns an element by matching text.
Optionally, provide a tag_name to narrow down the search to an
element with the given tag. (Eg: a, button, div, script, span)"""
self.__add_light_pause()
time_now = time.time()
self.assert_text(text, timeout=timeout)
spent = int(time.time() - time_now)
remaining = 1 + timeout - spent
if tag_name:
self.assert_element(tag_name, timeout=remaining)
elements = self.loop.run_until_complete(
self.page.find_elements_by_text(text=text)
)
if tag_name:
tag_name = tag_name.lower().strip()
for element in elements:
if element and not tag_name:
element = self.__add_sync_methods(element)
return self.__add_sync_methods(element)
elif (
element
and tag_name in element.tag_name.lower()
and text.strip() in element.text
):
element = self.__add_sync_methods(element)
return self.__add_sync_methods(element)
elif (
element.parent
and tag_name in element.parent.tag_name.lower()
and text.strip() in element.parent.text
):
element = self.__add_sync_methods(element.parent)
return self.__add_sync_methods(element)
elif (
element.parent.parent
and tag_name in element.parent.parent.tag_name.lower()
and text.strip() in element.parent.parent.text
):
element = self.__add_sync_methods(element.parent.parent)
return self.__add_sync_methods(element)
plural = "s"
if timeout == 1:
plural = ""
raise Exception(
"Text {%s} with tag {%s} was not found after %s second%s!"
% (text, tag_name, timeout, plural)
)

def find_all(self, selector, timeout=settings.SMALL_TIMEOUT):
self.__add_light_pause()
selector = self.__convert_to_css_if_xpath(selector)
Expand All @@ -177,26 +228,48 @@ def find_all(self, selector, timeout=settings.SMALL_TIMEOUT):
for element in elements:
element = self.__add_sync_methods(element)
updated_elements.append(element)
self.__slow_mode_pause_if_set()
return updated_elements

def find_elements_by_text(self, text, tag_name=None):
"""Returns a list of elements by matching text.
Optionally, provide a tag_name to narrow down the search
to only elements with the given tag. (Eg: a, div, script, span)"""
Optionally, provide a tag_name to narrow down the search to only
elements with the given tag. (Eg: a, button, div, script, span)"""
self.__add_light_pause()
elements = self.loop.run_until_complete(
self.page.find_elements_by_text(text=text)
)
updated_elements = []
if tag_name:
tag_name = tag_name.lower().strip()
for element in elements:
if (
not tag_name
or tag_name.lower().strip() in element.tag_name.lower().strip()
if element and not tag_name:
element = self.__add_sync_methods(element)
if element not in updated_elements:
updated_elements.append(element)
elif (
element
and tag_name in element.tag_name.lower()
and text.strip() in element.text
):
element = self.__add_sync_methods(element)
updated_elements.append(element)
self.__slow_mode_pause_if_set()
if element not in updated_elements:
updated_elements.append(element)
elif (
element.parent
and tag_name in element.parent.tag_name.lower()
and text.strip() in element.parent.text
):
element = self.__add_sync_methods(element.parent)
if element not in updated_elements:
updated_elements.append(element)
elif (
element.parent.parent
and tag_name in element.parent.parent.tag_name.lower()
and text.strip() in element.parent.parent.text
):
element = self.__add_sync_methods(element.parent.parent)
if element not in updated_elements:
updated_elements.append(element)
return updated_elements

def select(self, selector, timeout=settings.SMALL_TIMEOUT):
Expand Down Expand Up @@ -244,7 +317,6 @@ def select_all(self, selector, timeout=settings.SMALL_TIMEOUT):
for element in elements:
element = self.__add_sync_methods(element)
updated_elements.append(element)
self.__slow_mode_pause_if_set()
return updated_elements

def find_elements(self, selector, timeout=settings.SMALL_TIMEOUT):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
"pynose>=1.5.3",
'platformdirs>=4.3.6',
'typing-extensions>=4.12.2',
"sbvirtualdisplay>=1.3.0",
"sbvirtualdisplay>=1.3.1",
"six>=1.17.0",
'parse>=1.20.2',
'parse-type>=0.6.4',
Expand Down

0 comments on commit 6e0c8c5

Please sign in to comment.