This method clears an element's text field. A clear() is already included with most methods that type text, such as self.type(), self.update_text(), etc. Does not use Demo Mode highlights, mainly because we expect that some users will be calling an unnecessary clear()
(self, selector, by="css selector", timeout=None)
| 1196 | self.__demo_mode_pause_if_active() |
| 1197 | |
| 1198 | def clear(self, selector, by="css selector", timeout=None): |
| 1199 | """This method clears an element's text field. |
| 1200 | A clear() is already included with most methods that type text, |
| 1201 | such as self.type(), self.update_text(), etc. |
| 1202 | Does not use Demo Mode highlights, mainly because we expect |
| 1203 | that some users will be calling an unnecessary clear() before |
| 1204 | calling a method that already includes clear() as part of it. |
| 1205 | In case websites trigger an autofill after clearing a field, |
| 1206 | add backspaces to make sure autofill doesn't undo the clear. |
| 1207 | @Params |
| 1208 | selector - The selector of the text field. |
| 1209 | by - The type of selector to search by. (Default: "css selector") |
| 1210 | timeout - How long to wait for the selector to be visible.""" |
| 1211 | self.__check_scope() |
| 1212 | if not timeout: |
| 1213 | timeout = settings.LARGE_TIMEOUT |
| 1214 | if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT: |
| 1215 | timeout = self.__get_new_timeout(timeout) |
| 1216 | selector, by = self.__recalculate_selector(selector, by) |
| 1217 | if self.__is_cdp_swap_needed(): |
| 1218 | self.cdp.clear_input(selector) |
| 1219 | return |
| 1220 | if self.__is_shadow_selector(selector): |
| 1221 | self.__shadow_clear(selector, timeout) |
| 1222 | return |
| 1223 | element = self.wait_for_element_visible( |
| 1224 | selector, by=by, timeout=timeout |
| 1225 | ) |
| 1226 | self.scroll_to(selector, by=by, timeout=timeout) |
| 1227 | try: |
| 1228 | element.clear() |
| 1229 | backspaces = Keys.BACK_SPACE * 42 # Autofill Defense |
| 1230 | element.send_keys(backspaces) |
| 1231 | except (Stale_Exception, ENI_Exception): |
| 1232 | self.wait_for_ready_state_complete() |
| 1233 | time.sleep(0.16) |
| 1234 | element = self.wait_for_element_visible( |
| 1235 | selector, by=by, timeout=timeout |
| 1236 | ) |
| 1237 | element.clear() |
| 1238 | with suppress(Exception): |
| 1239 | backspaces = Keys.BACK_SPACE * 42 # Autofill Defense |
| 1240 | element.send_keys(backspaces) |
| 1241 | except Exception: |
| 1242 | element.clear() |
| 1243 | |
| 1244 | def focus(self, selector, by="css selector", timeout=None): |
| 1245 | """Make the current page focus on an interactable element. |