Skip to content

Commit bdd4a96

Browse files
committed
Add new method: get_select_options(dropdown_selector)
1 parent 3e34051 commit bdd4a96

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

help_docs/method_summary.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ self.select_option_by_value(
183183
dropdown_by="css selector",
184184
timeout=None)
185185

186+
self.get_select_options(
187+
dropdown_selector, attribute="text",
188+
by="css selector", timeout=None)
189+
186190
self.load_html_string(html_string, new_page=True)
187191

188192
self.set_content(html_string, new_page=False)

seleniumbase/fixtures/base_case.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2585,6 +2585,47 @@ def select_option_by_value(
25852585
timeout=timeout,
25862586
)
25872587

2588+
def get_select_options(
2589+
self,
2590+
dropdown_selector,
2591+
attribute="text",
2592+
by="css selector",
2593+
timeout=None,
2594+
):
2595+
"""Returns a list of select options as attribute text (configurable).
2596+
@Params
2597+
dropdown_selector - The selector of the "select" element.
2598+
attribute - Choose from "text", "index", "value", or None (elements).
2599+
by - The "by" of the "select" selector to use. Default: "css selector".
2600+
timeout - Time to wait for "select". If None: settings.SMALL_TIMEOUT.
2601+
"""
2602+
self.wait_for_ready_state_complete()
2603+
if not timeout:
2604+
timeout = settings.SMALL_TIMEOUT
2605+
if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:
2606+
timeout = self.__get_new_timeout(timeout)
2607+
selector = dropdown_selector
2608+
allowed_attributes = ["text", "index", "value", None]
2609+
if attribute not in allowed_attributes:
2610+
raise Exception("The attribute must be in %s" % allowed_attributes)
2611+
selector, by = self.__recalculate_selector(selector, by)
2612+
element = self.wait_for_element(selector, by=by, timeout=timeout)
2613+
if element.tag_name != "select":
2614+
raise Exception(
2615+
'Element tag_name for get_select_options(selector) must be a '
2616+
'"select"! Actual tag_name found was: "%s"' % element.tag_name
2617+
)
2618+
if by != "css selector":
2619+
selector = self.convert_to_css_selector(selector, by=by)
2620+
option_selector = selector + " option"
2621+
option_elements = self.find_elements(option_selector)
2622+
if not attribute:
2623+
return option_elements
2624+
elif attribute == "text":
2625+
return [e.text for e in option_elements]
2626+
else:
2627+
return [e.get_attribute(attribute) for e in option_elements]
2628+
25882629
def load_html_string(self, html_string, new_page=True):
25892630
"""Loads an HTML string into the web browser.
25902631
If new_page==True, the page will switch to: "data:text/html,"

0 commit comments

Comments
 (0)