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

Method remove_element

seleniumbase/fixtures/base_case.py:7407–7439  ·  view source on GitHub ↗

Remove the first element on the page that matches the selector.

(self, selector, by="css selector")

Source from the content-addressed store, hash-verified

7405 self.execute_script(script)
7406
7407 def remove_element(self, selector, by="css selector"):
7408 """Remove the first element on the page that matches the selector."""
7409 self.__check_scope()
7410 if self.__is_cdp_swap_needed():
7411 self.cdp.remove_element(selector)
7412 return
7413 element = None
7414 with suppress(Exception):
7415 self.wait_for_element_visible("body", timeout=1.5)
7416 element = self.wait_for_element_present(
7417 selector, by=by, timeout=0.5
7418 )
7419 selector, by = self.__recalculate_selector(selector, by)
7420 css_selector = self.convert_to_css_selector(selector, by=by)
7421 if ":contains(" in css_selector and element:
7422 script = (
7423 'const e = arguments[0];'
7424 'e.parentElement.removeChild(e);'
7425 )
7426 self.execute_script(script, element)
7427 elif ":contains(" in css_selector and not element:
7428 selector = self.__make_css_match_first_element_only(css_selector)
7429 script = """jQuery('%s').remove();""" % selector
7430 self.safe_execute_script(script)
7431 else:
7432 css_selector = re.escape(css_selector) # Add "\\" to special chars
7433 css_selector = self.__escape_quotes_if_needed(css_selector)
7434 script = (
7435 'const e = document.querySelector("%s");'
7436 'e.parentElement.removeChild(e);'
7437 % css_selector
7438 )
7439 self.execute_script(script)
7440
7441 def remove_elements(self, selector, by="css selector"):
7442 """Remove all elements on the page that match the selector."""

Callers

nothing calls this directly

Calls 10

__check_scopeMethod · 0.95
__is_cdp_swap_neededMethod · 0.95
execute_scriptMethod · 0.95
safe_execute_scriptMethod · 0.95

Tested by

no test coverage detected