This method uses JavaScript to set an element's textContent. If the element is an input or textarea, sets the value instead.
(
self, selector, text, by="css selector", timeout=None, scroll=False
)
| 9224 | self.set_text_content(selector, text, by=by, timeout=timeout) |
| 9225 | |
| 9226 | def set_text_content( |
| 9227 | self, selector, text, by="css selector", timeout=None, scroll=False |
| 9228 | ): |
| 9229 | """This method uses JavaScript to set an element's textContent. |
| 9230 | If the element is an input or textarea, sets the value instead.""" |
| 9231 | self.__check_scope() |
| 9232 | if not timeout: |
| 9233 | timeout = settings.LARGE_TIMEOUT |
| 9234 | if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT: |
| 9235 | timeout = self.__get_new_timeout(timeout) |
| 9236 | selector, by = self.__recalculate_selector(selector, by) |
| 9237 | element = None |
| 9238 | if self.__is_cdp_swap_needed(): |
| 9239 | element = self.cdp.select(selector, timeout=timeout) |
| 9240 | else: |
| 9241 | self.wait_for_ready_state_complete() |
| 9242 | element = page_actions.wait_for_element_present( |
| 9243 | self.driver, selector, by, timeout |
| 9244 | ) |
| 9245 | try: |
| 9246 | if element.tag_name.lower() in ["input", "textarea"]: |
| 9247 | self.js_update_text(selector, text, by=by, timeout=timeout) |
| 9248 | return |
| 9249 | except (Stale_Exception, ENI_Exception): |
| 9250 | time.sleep(0.16) |
| 9251 | if self.__is_cdp_swap_needed(): |
| 9252 | element = self.cdp.select(selector, timeout=timeout) |
| 9253 | else: |
| 9254 | self.wait_for_ready_state_complete() |
| 9255 | element = page_actions.wait_for_element_present( |
| 9256 | self.driver, selector, by, timeout |
| 9257 | ) |
| 9258 | if element.tag_name.lower() in ["input", "textarea"]: |
| 9259 | self.js_update_text(selector, text, by=by, timeout=timeout) |
| 9260 | return |
| 9261 | original_selector = selector |
| 9262 | css_selector = self.convert_to_css_selector(selector, by=by) |
| 9263 | if scroll: |
| 9264 | self.__demo_mode_highlight_if_active(original_selector, by) |
| 9265 | if not self.demo_mode and not self.slow_mode: |
| 9266 | self.scroll_to(original_selector, by=by, timeout=timeout) |
| 9267 | text = self.__get_type_checked_text(text) |
| 9268 | value = re.escape(text) |
| 9269 | value = self.__escape_quotes_if_needed(value) |
| 9270 | css_selector = re.escape(css_selector) # Add "\\" to special chars |
| 9271 | css_selector = self.__escape_quotes_if_needed(css_selector) |
| 9272 | if ":contains\\(" not in css_selector: |
| 9273 | script = """document.querySelector('%s').textContent='%s';""" % ( |
| 9274 | css_selector, |
| 9275 | value, |
| 9276 | ) |
| 9277 | self.execute_script(script) |
| 9278 | else: |
| 9279 | script = """arguments[0].textContent='%s';""" % value |
| 9280 | self.execute_script(script, element) |
| 9281 | self.__demo_mode_pause_if_active() |
| 9282 | |
| 9283 | def jquery_update_text( |