This method uses JavaScript to update a text field.
(
self, selector, text, by="css selector", timeout=None, scroll=True
)
| 9072 | return js_utils.convert_to_css_selector(selector, by) |
| 9073 | |
| 9074 | def set_value( |
| 9075 | self, selector, text, by="css selector", timeout=None, scroll=True |
| 9076 | ): |
| 9077 | """This method uses JavaScript to update a text field.""" |
| 9078 | self.__check_scope() |
| 9079 | if not timeout: |
| 9080 | timeout = settings.LARGE_TIMEOUT |
| 9081 | if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT: |
| 9082 | timeout = self.__get_new_timeout(timeout) |
| 9083 | selector, by = self.__recalculate_selector(selector, by, xp_ok=False) |
| 9084 | if self.__is_cdp_swap_needed(): |
| 9085 | self.cdp.set_value(selector, text) |
| 9086 | return |
| 9087 | self.wait_for_ready_state_complete() |
| 9088 | self.wait_for_element_present(selector, by=by, timeout=timeout) |
| 9089 | original_selector = selector |
| 9090 | css_selector = self.convert_to_css_selector(selector, by=by) |
| 9091 | self.__demo_mode_highlight_if_active(original_selector, by) |
| 9092 | if scroll and not self.demo_mode and not self.slow_mode: |
| 9093 | self.scroll_to(original_selector, by=by, timeout=timeout) |
| 9094 | if self.__needs_minimum_wait(): |
| 9095 | time.sleep(0.04) |
| 9096 | if not scroll and not self.demo_mode and not self.slow_mode: |
| 9097 | if self.__needs_minimum_wait(): |
| 9098 | time.sleep(0.06) |
| 9099 | text = self.__get_type_checked_text(text) |
| 9100 | value = re.escape(text) |
| 9101 | value = self.__escape_quotes_if_needed(value) |
| 9102 | pre_escape_css_selector = css_selector |
| 9103 | css_selector = re.escape(css_selector) # Add "\\" to special chars |
| 9104 | css_selector = self.__escape_quotes_if_needed(css_selector) |
| 9105 | the_type = None |
| 9106 | if ":contains\\(" not in css_selector: |
| 9107 | get_type_script = ( |
| 9108 | """return document.querySelector('%s').getAttribute('type');""" |
| 9109 | % css_selector |
| 9110 | ) |
| 9111 | the_type = self.execute_script(get_type_script) # Used later |
| 9112 | if self.recorder_mode and self.__current_url_is_recordable(): |
| 9113 | if self.get_session_storage_item("pause_recorder") == "no": |
| 9114 | time_stamp = self.execute_script("return Date.now();") |
| 9115 | origin = self.get_origin() |
| 9116 | sel_tex = [pre_escape_css_selector, text] |
| 9117 | if the_type == "range" and ":contains\\(" not in css_selector: |
| 9118 | action = ["s_val", sel_tex, origin, time_stamp] |
| 9119 | else: |
| 9120 | action = ["js_ty", sel_tex, origin, time_stamp] |
| 9121 | self.__extra_actions.append(action) |
| 9122 | if ":contains\\(" not in css_selector: |
| 9123 | script = """document.querySelector('%s').value='%s';""" % ( |
| 9124 | css_selector, |
| 9125 | value, |
| 9126 | ) |
| 9127 | self.execute_script(script) |
| 9128 | else: |
| 9129 | element = self.wait_for_element_present( |
| 9130 | original_selector, by=by, timeout=timeout |
| 9131 | ) |
no test coverage detected