The more-reliable version of driver.send_keys() Similar to update_text(), but won't clear the text field first.
(self, selector, text, by="css selector", timeout=None)
| 1015 | self.__slow_mode_pause_if_active() |
| 1016 | |
| 1017 | def add_text(self, selector, text, by="css selector", timeout=None): |
| 1018 | """The more-reliable version of driver.send_keys() |
| 1019 | Similar to update_text(), but won't clear the text field first.""" |
| 1020 | self.__check_scope() |
| 1021 | if not timeout: |
| 1022 | timeout = settings.LARGE_TIMEOUT |
| 1023 | if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT: |
| 1024 | timeout = self.__get_new_timeout(timeout) |
| 1025 | selector, by = self.__recalculate_selector(selector, by) |
| 1026 | if self.__is_cdp_swap_needed(): |
| 1027 | self.cdp.send_keys(selector, text) |
| 1028 | return |
| 1029 | if self.__is_shadow_selector(selector): |
| 1030 | self.__shadow_type(selector, text, timeout, clear_first=False) |
| 1031 | return |
| 1032 | if selector == "html" and text in ["\n", Keys.ENTER, Keys.RETURN]: |
| 1033 | # This is a shortcut for calling self.click_active_element(). |
| 1034 | # Use after "\t" or Keys.TAB to cycle through elements first. |
| 1035 | self.click_active_element() |
| 1036 | return |
| 1037 | element = self.wait_for_element_present( |
| 1038 | selector, by=by, timeout=timeout |
| 1039 | ) |
| 1040 | if ( |
| 1041 | selector == "html" and text.count("\t") >= 1 |
| 1042 | and text.count("\n") == 1 and text.endswith("\n") |
| 1043 | and text.replace("\t", "").replace("\n", "").replace(" ", "") == "" |
| 1044 | ): |
| 1045 | # Shortcut to send multiple tabs followed by click_active_element() |
| 1046 | self.wait_for_ready_state_complete() |
| 1047 | element.send_keys(Keys.TAB * text.count("\t")) |
| 1048 | self.click_active_element() |
| 1049 | return |
| 1050 | self.__demo_mode_highlight_if_active(selector, by) |
| 1051 | if not self.demo_mode and not self.slow_mode: |
| 1052 | self.__scroll_to_element(element, selector, by) |
| 1053 | pre_action_url = None |
| 1054 | if self.demo_mode: |
| 1055 | with suppress(Exception): |
| 1056 | pre_action_url = self.driver.current_url |
| 1057 | text = self.__get_type_checked_text(text) |
| 1058 | try: |
| 1059 | if not text.endswith("\n"): |
| 1060 | element.send_keys(text) |
| 1061 | else: |
| 1062 | element.send_keys(text[:-1]) |
| 1063 | if self.slow_mode or self.demo_mode: |
| 1064 | self.__demo_mode_pause_if_active(tiny=True) |
| 1065 | else: |
| 1066 | time.sleep(0.0135) |
| 1067 | element.send_keys(Keys.RETURN) |
| 1068 | if settings.WAIT_FOR_RSC_ON_PAGE_LOADS: |
| 1069 | self.wait_for_ready_state_complete() |
| 1070 | except (Stale_Exception, ENI_Exception): |
| 1071 | self.wait_for_ready_state_complete() |
| 1072 | time.sleep(0.16) |
| 1073 | element = self.wait_for_element_visible( |
| 1074 | selector, by=by, timeout=timeout |