Add "NoSuchOptionException" for missing "<select>" options

This commit is contained in:
Michael Mintz 2023-07-12 13:44:17 -04:00
parent be477faa56
commit 6d8187a315
3 changed files with 40 additions and 3 deletions

View File

@ -1,5 +1,6 @@
""" SeleniumBase Exceptions
NoSuchFileException => Called when self.assert_downloaded_file(...) fails.
NoSuchOptionException => Called when select_option_by_*() lacks the option.
NotConnectedException => Called when Internet is not reachable when needed.
NotUsingChromeException => Used by Chrome-only methods if not using Chrome.
NotUsingChromiumException => Used by Chromium-only methods if not Chromium.
@ -15,6 +16,10 @@ class NoSuchFileException(Exception):
pass
class NoSuchOptionException(Exception):
pass
class NotConnectedException(Exception):
pass

View File

@ -2829,11 +2829,38 @@ class BaseCase(unittest.TestCase):
except Exception:
self.wait_for_ready_state_complete()
if option_by == "index":
try:
Select(element).select_by_index(option)
except Exception:
msg = (
"Element {%s} has no selectable index option {%s}!"
% (dropdown_selector, option)
)
page_actions.timeout_exception(
"NoSuchOptionException", msg
)
elif option_by == "value":
try:
Select(element).select_by_value(option)
except Exception:
msg = (
"Element {%s} has no selectable value option {%s}!"
% (dropdown_selector, option)
)
page_actions.timeout_exception(
"NoSuchOptionException", msg
)
else:
try:
Select(element).select_by_visible_text(option)
except Exception:
msg = (
"Element {%s} has no selectable text option {%s}!"
% (dropdown_selector, option)
)
page_actions.timeout_exception(
"NoSuchOptionException", msg
)
time.sleep(0.05)
self.wait_for_ready_state_complete()
latest_window_count = len(self.driver.window_handles)

View File

@ -123,6 +123,7 @@ def format_exc(exception, message):
from selenium.common.exceptions import NoSuchFrameException
from selenium.common.exceptions import NoSuchWindowException
from seleniumbase.common.exceptions import NoSuchFileException
from seleniumbase.common.exceptions import NoSuchOptionException
from seleniumbase.common.exceptions import TextNotVisibleException
from seleniumbase.common import exceptions
@ -161,6 +162,10 @@ def format_exc(exception, message):
exc = exceptions.NoSuchFileException
elif exception == "NoSuchFileException":
exc = exceptions.NoSuchFileException
elif exception == NoSuchOptionException:
exc = exceptions.NoSuchOptionException
elif exception == "NoSuchOptionException":
exc = exceptions.NoSuchOptionException
elif type(exception) is str:
exc = Exception
message = "%s: %s" % (exception, message)