MCPcopy Create free account
hub / github.com/seleniumbase/SeleniumBase / find_element

Method find_element

seleniumbase/core/sb_cdp.py:260–317  ·  view source on GitHub ↗

Similar to select(), but also finds elements by text content. When using text-based searches, if best_match=False, then will find the first element with the text. If best_match=True, then if multiple elements have that text, then will use the element with the closest

(self, selector, best_match=False, timeout=None)

Source from the content-addressed store, hash-verified

258 self.page.add_handler(event, handler)
259
260 def find_element(self, selector, best_match=False, timeout=None):
261 """Similar to select(), but also finds elements by text content.
262 When using text-based searches, if best_match=False, then will
263 find the first element with the text. If best_match=True, then
264 if multiple elements have that text, then will use the element
265 with the closest text-length to the text being searched for."""
266 if not timeout:
267 timeout = settings.SMALL_TIMEOUT
268 self.__add_light_pause()
269 selector = self.__convert_to_css_if_xpath(selector)
270 early_failure = False
271 if (":contains(") in selector:
272 selector, _ = page_utils.recalculate_selector(
273 selector, by="css selector", xp_ok=True
274 )
275 failure = False
276 try:
277 if early_failure:
278 raise Exception("Failed!")
279 if (
280 "contains(" not in selector
281 and not page_utils.is_xpath_selector(selector)
282 and not re.findall(r"\.\s", selector)
283 and not re.findall(r"\.$", selector)
284 and not re.findall(r"#\s", selector)
285 and not re.findall(r"#$", selector)
286 and (
287 selector in ["html", "body"]
288 or "[" in selector
289 or re.findall(r"\.\S", selector)
290 or re.findall(r"#\S", selector)
291 or " > " in selector
292 )
293 ):
294 element = self.loop.run_until_complete(
295 self.page.select(selector, timeout=timeout)
296 )
297 else:
298 element = self.loop.run_until_complete(
299 self.page.find(
300 selector, best_match=best_match, timeout=timeout
301 )
302 )
303 except Exception:
304 failure = True
305 plural = "s"
306 if timeout == 1:
307 plural = ""
308 message = "\n Element {%s} was not found after %s second%s!" % (
309 selector,
310 timeout,
311 plural,
312 )
313 if failure:
314 raise Exception(message)
315 element = self.__add_sync_methods(element)
316 self.__slow_mode_pause_if_set()
317 return element

Callers 15

find_element_by_textMethod · 0.95
selectMethod · 0.95
clickMethod · 0.95
click_if_visibleMethod · 0.95
mouse_clickMethod · 0.95
nested_clickMethod · 0.95
get_nested_elementMethod · 0.95
select_option_by_textMethod · 0.95
flashMethod · 0.95
highlightMethod · 0.95

Calls 6

__add_light_pauseMethod · 0.95
__add_sync_methodsMethod · 0.95
findMethod · 0.80
selectMethod · 0.45

Tested by 6

test_add_item_to_cartMethod · 0.36
test_add_item_to_cartMethod · 0.36
test_add_item_to_cartMethod · 0.36
test_add_item_to_cartMethod · 0.36
test_add_item_to_cartMethod · 0.36