Skip to content

Commit 6e0c8c5

Browse files
authored
Merge pull request #3333 from seleniumbase/cdp-mode-patch-19
CDP Mode - Patch 19
2 parents 992212c + 79fbbce commit 6e0c8c5

File tree

6 files changed

+86
-12
lines changed

6 files changed

+86
-12
lines changed

examples/cdp_mode/ReadMe.md

+1
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ sb.cdp.add_handler(event, handler)
313313
sb.cdp.find_element(selector)
314314
sb.cdp.find(selector)
315315
sb.cdp.locator(selector)
316+
sb.cdp.find_element_by_text(text, tag_name=None)
316317
sb.cdp.find_all(selector)
317318
sb.cdp.find_elements_by_text(text, tag_name=None)
318319
sb.cdp.select(selector)

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ mycdp>=1.1.0
1414
pynose>=1.5.3
1515
platformdirs>=4.3.6
1616
typing-extensions>=4.12.2
17-
sbvirtualdisplay>=1.3.0
17+
sbvirtualdisplay>=1.3.1
1818
six>=1.17.0
1919
parse>=1.20.2
2020
parse-type>=0.6.4

seleniumbase/__version__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# seleniumbase package
2-
__version__ = "4.33.8"
2+
__version__ = "4.33.9"

seleniumbase/core/browser_launcher.py

+1
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,7 @@ def uc_open_with_cdp_mode(driver, url=None):
600600
cdp.find_element = CDPM.find_element
601601
cdp.find = CDPM.find_element
602602
cdp.locator = CDPM.find_element
603+
cdp.find_element_by_text = CDPM.find_element_by_text
603604
cdp.find_all = CDPM.find_all
604605
cdp.find_elements_by_text = CDPM.find_elements_by_text
605606
cdp.select = CDPM.select

seleniumbase/core/sb_cdp.py

+81-9
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,57 @@ def find_element(
167167
self.__slow_mode_pause_if_set()
168168
return element
169169

170+
def find_element_by_text(
171+
self, text, tag_name=None, timeout=settings.SMALL_TIMEOUT
172+
):
173+
"""Returns an element by matching text.
174+
Optionally, provide a tag_name to narrow down the search to an
175+
element with the given tag. (Eg: a, button, div, script, span)"""
176+
self.__add_light_pause()
177+
time_now = time.time()
178+
self.assert_text(text, timeout=timeout)
179+
spent = int(time.time() - time_now)
180+
remaining = 1 + timeout - spent
181+
if tag_name:
182+
self.assert_element(tag_name, timeout=remaining)
183+
elements = self.loop.run_until_complete(
184+
self.page.find_elements_by_text(text=text)
185+
)
186+
if tag_name:
187+
tag_name = tag_name.lower().strip()
188+
for element in elements:
189+
if element and not tag_name:
190+
element = self.__add_sync_methods(element)
191+
return self.__add_sync_methods(element)
192+
elif (
193+
element
194+
and tag_name in element.tag_name.lower()
195+
and text.strip() in element.text
196+
):
197+
element = self.__add_sync_methods(element)
198+
return self.__add_sync_methods(element)
199+
elif (
200+
element.parent
201+
and tag_name in element.parent.tag_name.lower()
202+
and text.strip() in element.parent.text
203+
):
204+
element = self.__add_sync_methods(element.parent)
205+
return self.__add_sync_methods(element)
206+
elif (
207+
element.parent.parent
208+
and tag_name in element.parent.parent.tag_name.lower()
209+
and text.strip() in element.parent.parent.text
210+
):
211+
element = self.__add_sync_methods(element.parent.parent)
212+
return self.__add_sync_methods(element)
213+
plural = "s"
214+
if timeout == 1:
215+
plural = ""
216+
raise Exception(
217+
"Text {%s} with tag {%s} was not found after %s second%s!"
218+
% (text, tag_name, timeout, plural)
219+
)
220+
170221
def find_all(self, selector, timeout=settings.SMALL_TIMEOUT):
171222
self.__add_light_pause()
172223
selector = self.__convert_to_css_if_xpath(selector)
@@ -177,26 +228,48 @@ def find_all(self, selector, timeout=settings.SMALL_TIMEOUT):
177228
for element in elements:
178229
element = self.__add_sync_methods(element)
179230
updated_elements.append(element)
180-
self.__slow_mode_pause_if_set()
181231
return updated_elements
182232

183233
def find_elements_by_text(self, text, tag_name=None):
184234
"""Returns a list of elements by matching text.
185-
Optionally, provide a tag_name to narrow down the search
186-
to only elements with the given tag. (Eg: a, div, script, span)"""
235+
Optionally, provide a tag_name to narrow down the search to only
236+
elements with the given tag. (Eg: a, button, div, script, span)"""
187237
self.__add_light_pause()
188238
elements = self.loop.run_until_complete(
189239
self.page.find_elements_by_text(text=text)
190240
)
191241
updated_elements = []
242+
if tag_name:
243+
tag_name = tag_name.lower().strip()
192244
for element in elements:
193-
if (
194-
not tag_name
195-
or tag_name.lower().strip() in element.tag_name.lower().strip()
245+
if element and not tag_name:
246+
element = self.__add_sync_methods(element)
247+
if element not in updated_elements:
248+
updated_elements.append(element)
249+
elif (
250+
element
251+
and tag_name in element.tag_name.lower()
252+
and text.strip() in element.text
196253
):
197254
element = self.__add_sync_methods(element)
198-
updated_elements.append(element)
199-
self.__slow_mode_pause_if_set()
255+
if element not in updated_elements:
256+
updated_elements.append(element)
257+
elif (
258+
element.parent
259+
and tag_name in element.parent.tag_name.lower()
260+
and text.strip() in element.parent.text
261+
):
262+
element = self.__add_sync_methods(element.parent)
263+
if element not in updated_elements:
264+
updated_elements.append(element)
265+
elif (
266+
element.parent.parent
267+
and tag_name in element.parent.parent.tag_name.lower()
268+
and text.strip() in element.parent.parent.text
269+
):
270+
element = self.__add_sync_methods(element.parent.parent)
271+
if element not in updated_elements:
272+
updated_elements.append(element)
200273
return updated_elements
201274

202275
def select(self, selector, timeout=settings.SMALL_TIMEOUT):
@@ -244,7 +317,6 @@ def select_all(self, selector, timeout=settings.SMALL_TIMEOUT):
244317
for element in elements:
245318
element = self.__add_sync_methods(element)
246319
updated_elements.append(element)
247-
self.__slow_mode_pause_if_set()
248320
return updated_elements
249321

250322
def find_elements(self, selector, timeout=settings.SMALL_TIMEOUT):

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@
163163
"pynose>=1.5.3",
164164
'platformdirs>=4.3.6',
165165
'typing-extensions>=4.12.2',
166-
"sbvirtualdisplay>=1.3.0",
166+
"sbvirtualdisplay>=1.3.1",
167167
"six>=1.17.0",
168168
'parse>=1.20.2',
169169
'parse-type>=0.6.4',

0 commit comments

Comments
 (0)