@@ -530,8 +530,7 @@ def update_text(
530
530
pass # Clearing the text field first might not be necessary
531
531
self.__demo_mode_pause_if_active(tiny=True)
532
532
pre_action_url = self.driver.current_url
533
- if type(text) is int or type(text) is float:
534
- text = str(text)
533
+ text = self.__get_type_checked_text(text)
535
534
try:
536
535
if not text.endswith("\n"):
537
536
element.send_keys(text)
@@ -590,8 +589,7 @@ def add_text(self, selector, text, by=By.CSS_SELECTOR, timeout=None):
590
589
if not self.demo_mode and not self.slow_mode:
591
590
self.__scroll_to_element(element, selector, by)
592
591
pre_action_url = self.driver.current_url
593
- if type(text) is int or type(text) is float:
594
- text = str(text)
592
+ text = self.__get_type_checked_text(text)
595
593
try:
596
594
if not text.endswith("\n"):
597
595
element.send_keys(text)
@@ -988,6 +986,7 @@ def click_link_text(self, link_text, timeout=None):
988
986
timeout = self.__get_new_timeout(timeout)
989
987
pre_action_url = self.driver.current_url
990
988
pre_window_count = len(self.driver.window_handles)
989
+ link_text = self.__get_type_checked_text(link_text)
991
990
if self.browser == "phantomjs":
992
991
if self.is_link_text_visible(link_text):
993
992
element = self.wait_for_link_text_visible(
@@ -1109,6 +1108,7 @@ def click_partial_link_text(self, partial_link_text, timeout=None):
1109
1108
timeout = settings.SMALL_TIMEOUT
1110
1109
if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:
1111
1110
timeout = self.__get_new_timeout(timeout)
1111
+ partial_link_text = self.__get_type_checked_text(partial_link_text)
1112
1112
if self.browser == "phantomjs":
1113
1113
if self.is_partial_link_text_visible(partial_link_text):
1114
1114
element = self.wait_for_partial_link_text(partial_link_text)
@@ -4765,6 +4765,32 @@ def __fix_unicode_conversion(self, text):
4765
4765
text = text.replace("\xe2\xbd\x85", "\xe6\x96\xb9")
4766
4766
return text
4767
4767
4768
+ def __get_type_checked_text(self, text):
4769
+ """ Do type-checking on text. Then return it when valid.
4770
+ If the text is acceptable, return the text or str(text).
4771
+ If the text is not acceptable, raise a Python Exception.
4772
+ """
4773
+ if type(text) is str:
4774
+ return text
4775
+ elif type(text) is int or type(text) is float:
4776
+ return str(text) # Convert num to string
4777
+ elif type(text) is bool:
4778
+ raise Exception("text must be a string! Boolean found!")
4779
+ elif type(text).__name__ == "NoneType":
4780
+ raise Exception("text must be a string! NoneType found!")
4781
+ elif type(text) is list:
4782
+ raise Exception("text must be a string! List found!")
4783
+ elif type(text) is tuple:
4784
+ raise Exception("text must be a string! Tuple found!")
4785
+ elif type(text) is set:
4786
+ raise Exception("text must be a string! Set found!")
4787
+ elif type(text) is dict:
4788
+ raise Exception("text must be a string! Dict found!")
4789
+ elif not python3 and type(text) is unicode: # noqa: F821
4790
+ return text # (For old Python versions with unicode)
4791
+ else:
4792
+ return str(text)
4793
+
4768
4794
def get_pdf_text(
4769
4795
self,
4770
4796
pdf,
@@ -5703,8 +5729,7 @@ def set_value(
5703
5729
self.__demo_mode_highlight_if_active(orginal_selector, by)
5704
5730
if scroll and not self.demo_mode and not self.slow_mode:
5705
5731
self.scroll_to(orginal_selector, by=by, timeout=timeout)
5706
- if type(text) is int or type(text) is float:
5707
- text = str(text)
5732
+ text = self.__get_type_checked_text(text)
5708
5733
value = re.escape(text)
5709
5734
value = self.__escape_quotes_if_needed(value)
5710
5735
pre_escape_css_selector = css_selector
@@ -5765,8 +5790,7 @@ def js_update_text(self, selector, text, by=By.CSS_SELECTOR, timeout=None):
5765
5790
if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:
5766
5791
timeout = self.__get_new_timeout(timeout)
5767
5792
selector, by = self.__recalculate_selector(selector, by)
5768
- if type(text) is int or type(text) is float:
5769
- text = str(text)
5793
+ text = self.__get_type_checked_text(text)
5770
5794
self.set_value(selector, text, by=by, timeout=timeout)
5771
5795
if not text.endswith("\n"):
5772
5796
try:
@@ -5838,8 +5862,7 @@ def set_text_content(
5838
5862
self.__demo_mode_highlight_if_active(orginal_selector, by)
5839
5863
if not self.demo_mode and not self.slow_mode:
5840
5864
self.scroll_to(orginal_selector, by=by, timeout=timeout)
5841
- if type(text) is int or type(text) is float:
5842
- text = str(text)
5865
+ text = self.__get_type_checked_text(text)
5843
5866
value = re.escape(text)
5844
5867
value = self.__escape_quotes_if_needed(value)
5845
5868
css_selector = re.escape(css_selector) # Add "\\" to special chars
@@ -6185,8 +6208,7 @@ def __shadow_type(self, selector, text, timeout, clear_first=True):
6185
6208
element.send_keys(backspaces)
6186
6209
except Exception:
6187
6210
pass
6188
- if type(text) is int or type(text) is float:
6189
- text = str(text)
6211
+ text = self.__get_type_checked_text(text)
6190
6212
if not text.endswith("\n"):
6191
6213
element.send_keys(text)
6192
6214
if settings.WAIT_FOR_RSC_ON_PAGE_LOADS:
@@ -9390,6 +9412,7 @@ def wait_for_text_visible(
9390
9412
timeout = settings.LARGE_TIMEOUT
9391
9413
if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:
9392
9414
timeout = self.__get_new_timeout(timeout)
9415
+ text = self.__get_type_checked_text(text)
9393
9416
selector, by = self.__recalculate_selector(selector, by)
9394
9417
if self.__is_shadow_selector(selector):
9395
9418
return self.__wait_for_shadow_text_visible(
0 commit comments