Selects an HTML option by specification. Option specifications are by "text", "index", or "value". Defaults to "text" if option_by is unspecified or unknown.
(
self,
dropdown_selector,
option,
dropdown_by="css selector",
option_by="text",
timeout=None,
)
| 3068 | element.click() |
| 3069 | |
| 3070 | def __select_option( |
| 3071 | self, |
| 3072 | dropdown_selector, |
| 3073 | option, |
| 3074 | dropdown_by="css selector", |
| 3075 | option_by="text", |
| 3076 | timeout=None, |
| 3077 | ): |
| 3078 | """Selects an HTML <select> option by specification. |
| 3079 | Option specifications are by "text", "index", or "value". |
| 3080 | Defaults to "text" if option_by is unspecified or unknown.""" |
| 3081 | from selenium.webdriver.support.ui import Select |
| 3082 | |
| 3083 | self.__check_scope() |
| 3084 | if not timeout: |
| 3085 | timeout = settings.SMALL_TIMEOUT |
| 3086 | if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT: |
| 3087 | timeout = self.__get_new_timeout(timeout) |
| 3088 | dropdown_selector, dropdown_by = self.__recalculate_selector( |
| 3089 | dropdown_selector, dropdown_by |
| 3090 | ) |
| 3091 | self.wait_for_ready_state_complete() |
| 3092 | element = self.wait_for_element_present( |
| 3093 | dropdown_selector, by=dropdown_by, timeout=timeout |
| 3094 | ) |
| 3095 | try: |
| 3096 | element = self.wait_for_element_clickable( |
| 3097 | dropdown_selector, by=dropdown_by, timeout=1.8 |
| 3098 | ) |
| 3099 | except Exception: |
| 3100 | self.wait_for_ready_state_complete() |
| 3101 | if self.is_element_visible(dropdown_selector, by=dropdown_by): |
| 3102 | self.__demo_mode_highlight_if_active( |
| 3103 | dropdown_selector, dropdown_by |
| 3104 | ) |
| 3105 | pre_action_url = None |
| 3106 | with suppress(Exception): |
| 3107 | pre_action_url = self.driver.current_url |
| 3108 | pre_window_count = len(self.driver.window_handles) |
| 3109 | try: |
| 3110 | if option_by == "index": |
| 3111 | Select(element).select_by_index(option) |
| 3112 | elif option_by == "value": |
| 3113 | Select(element).select_by_value(option) |
| 3114 | else: |
| 3115 | Select(element).select_by_visible_text(option) |
| 3116 | time.sleep(0.05) |
| 3117 | self.wait_for_ready_state_complete() |
| 3118 | except Exception: |
| 3119 | time.sleep(0.25) |
| 3120 | self.wait_for_ready_state_complete() |
| 3121 | element = self.wait_for_element_present( |
| 3122 | dropdown_selector, by=dropdown_by, timeout=timeout |
| 3123 | ) |
| 3124 | try: |
| 3125 | element = self.wait_for_element_clickable( |
| 3126 | dropdown_selector, by=dropdown_by, timeout=1.8 |
| 3127 | ) |
no test coverage detected