Remove all elements on the page that match the selector.
(self, selector, by="css selector")
| 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.""" |
| 7443 | self.__check_scope() |
| 7444 | if self.__is_cdp_swap_needed(): |
| 7445 | self.cdp.remove_elements(selector) |
| 7446 | return |
| 7447 | with suppress(Exception): |
| 7448 | self.wait_for_element_visible("body", timeout=1.5) |
| 7449 | selector, by = self.__recalculate_selector(selector, by) |
| 7450 | css_selector = self.convert_to_css_selector(selector, by=by) |
| 7451 | if ":contains(" in css_selector: |
| 7452 | script = """jQuery('%s').remove();""" % css_selector |
| 7453 | self.safe_execute_script(script) |
| 7454 | else: |
| 7455 | css_selector = re.escape(css_selector) # Add "\\" to special chars |
| 7456 | css_selector = self.__escape_quotes_if_needed(css_selector) |
| 7457 | script = ( |
| 7458 | """var $elements = document.querySelectorAll('%s'); |
| 7459 | var index = 0, length = $elements.length; |
| 7460 | for(; index < length; index++){ |
| 7461 | $elements[index].remove();}""" |
| 7462 | % css_selector |
| 7463 | ) |
| 7464 | self.execute_script(script) |
| 7465 | |
| 7466 | def ad_block(self): |
| 7467 | """Block ads that appear on the current web page.""" |
nothing calls this directly
no test coverage detected