Skip to content

Commit 16c6103

Browse files
committed
Add method: "set_text_content()" to change text anywhere
1 parent ab924bb commit 16c6103

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4216,14 +4216,66 @@ def set_text(self, selector, text, by=By.CSS_SELECTOR, timeout=None):
42164216
JavaScript + send_keys are used to update a text field.
42174217
Performs self.set_value() and triggers event listeners.
42184218
If text ends in "\n", set_value() presses RETURN after.
4219-
Works faster than send_keys() alone due to the JS call."""
4219+
Works faster than send_keys() alone due to the JS call.
4220+
If not an input or textarea, sets textContent instead."""
42204221
self.__check_scope()
42214222
if not timeout:
42224223
timeout = settings.LARGE_TIMEOUT
42234224
if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:
42244225
timeout = self.__get_new_timeout(timeout)
42254226
selector, by = self.__recalculate_selector(selector, by)
4226-
self.js_update_text(selector, text, by=by, timeout=timeout)
4227+
self.wait_for_ready_state_complete()
4228+
element = page_actions.wait_for_element_present(
4229+
self.driver, selector, by, timeout
4230+
)
4231+
if element.tag_name == "input" or element.tag_name == "textarea":
4232+
self.js_update_text(selector, text, by=by, timeout=timeout)
4233+
else:
4234+
self.set_text_content(selector, text, by=by, timeout=timeout)
4235+
4236+
def set_text_content(
4237+
self, selector, text, by=By.CSS_SELECTOR, timeout=None, scroll=False
4238+
):
4239+
"""This method uses JavaScript to set an element's textContent.
4240+
If the element is an input or textarea, sets the value instead."""
4241+
self.__check_scope()
4242+
if not timeout:
4243+
timeout = settings.LARGE_TIMEOUT
4244+
if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:
4245+
timeout = self.__get_new_timeout(timeout)
4246+
selector, by = self.__recalculate_selector(selector, by)
4247+
self.wait_for_ready_state_complete()
4248+
element = page_actions.wait_for_element_present(
4249+
self.driver, selector, by, timeout
4250+
)
4251+
if element.tag_name == "input" or element.tag_name == "textarea":
4252+
self.js_update_text(selector, text, by=by, timeout=timeout)
4253+
return
4254+
orginal_selector = selector
4255+
css_selector = self.convert_to_css_selector(selector, by=by)
4256+
if scroll:
4257+
self.__demo_mode_highlight_if_active(orginal_selector, by)
4258+
if not self.demo_mode and not self.slow_mode:
4259+
self.scroll_to(orginal_selector, by=by, timeout=timeout)
4260+
if type(text) is int or type(text) is float:
4261+
text = str(text)
4262+
value = re.escape(text)
4263+
value = self.__escape_quotes_if_needed(value)
4264+
css_selector = re.escape(css_selector) # Add "\\" to special chars
4265+
css_selector = self.__escape_quotes_if_needed(css_selector)
4266+
if ":contains\\(" not in css_selector:
4267+
script = """document.querySelector('%s').textContent='%s';""" % (
4268+
css_selector,
4269+
value,
4270+
)
4271+
self.execute_script(script)
4272+
else:
4273+
script = """jQuery('%s')[0].textContent='%s';""" % (
4274+
css_selector,
4275+
value,
4276+
)
4277+
self.safe_execute_script(script)
4278+
self.__demo_mode_pause_if_active()
42274279

42284280
def jquery_update_text(
42294281
self, selector, text, by=By.CSS_SELECTOR, timeout=None

0 commit comments

Comments
 (0)