Skip to content

CDP Mode - Patch 19 #3333

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

Merged
merged 3 commits into from
Dec 13, 2024
Merged
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
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
Loading