Skip to content

Commit ab924bb

Browse files
committed
Add method: "get_value()" to get the value from an input field
1 parent 5a1c46d commit ab924bb

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4254,6 +4254,36 @@ def jquery_update_text(
42544254
element.send_keys("\n")
42554255
self.__demo_mode_pause_if_active()
42564256

4257+
def get_value(
4258+
self, selector, by=By.CSS_SELECTOR, timeout=None
4259+
):
4260+
"""This method uses JavaScript to get the value of an input field.
4261+
(Works on both input fields and textarea fields.)"""
4262+
self.__check_scope()
4263+
if not timeout:
4264+
timeout = settings.LARGE_TIMEOUT
4265+
if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:
4266+
timeout = self.__get_new_timeout(timeout)
4267+
selector, by = self.__recalculate_selector(selector, by)
4268+
self.wait_for_ready_state_complete()
4269+
self.wait_for_element_present(selector, by=by, timeout=timeout)
4270+
orginal_selector = selector
4271+
css_selector = self.convert_to_css_selector(selector, by=by)
4272+
self.__demo_mode_highlight_if_active(orginal_selector, by)
4273+
if not self.demo_mode and not self.slow_mode:
4274+
self.scroll_to(orginal_selector, by=by, timeout=timeout)
4275+
css_selector = re.escape(css_selector) # Add "\\" to special chars
4276+
css_selector = self.__escape_quotes_if_needed(css_selector)
4277+
if ":contains\\(" not in css_selector:
4278+
script = """return document.querySelector('%s').value;""" % (
4279+
css_selector
4280+
)
4281+
value = self.execute_script(script)
4282+
else:
4283+
script = """return jQuery('%s')[0].value;""" % css_selector
4284+
value = self.safe_execute_script(script)
4285+
return value
4286+
42574287
def set_time_limit(self, time_limit):
42584288
self.__check_scope()
42594289
if time_limit:

0 commit comments

Comments
 (0)