Remove the first element on the page that matches the selector.
(self, selector, by="css selector")
| 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.""" |
nothing calls this directly
no test coverage detected