Skip to content

Commit

Permalink
[py]: Tweaks to select.py for pythonic naming and types (#10756)
Browse files Browse the repository at this point in the history
  • Loading branch information
symonk committed Jun 9, 2022
1 parent 890f74e commit 05467b6
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions py/selenium/webdriver/support/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@

class Select:

def __init__(self, webelement):
def __init__(self, webelement) -> None:
"""
Constructor. A check is made that the given element is, indeed, a SELECT tag. If it is not,
then an UnexpectedTagNameException is thrown.
:Args:
- webelement - element SELECT element to wrap
- webelement - SELECT element to wrap
Example:
from selenium.webdriver.support.ui import Select \n
Expand All @@ -49,11 +49,7 @@ def options(self):
@property
def all_selected_options(self):
"""Returns a list of all selected options belonging to this select tag"""
ret = []
for opt in self.options:
if opt.is_selected():
ret.append(opt)
return ret
return [opt for opt in self.options if opt.is_selected()]

@property
def first_selected_option(self):
Expand All @@ -75,11 +71,11 @@ def select_by_value(self, value):
throws NoSuchElementException If there is no option with specified value in SELECT
"""
css = "option[value =%s]" % self._escapeString(value)
css = "option[value =%s]" % self._escape_string(value)
opts = self._el.find_elements(By.CSS_SELECTOR, css)
matched = False
for opt in opts:
self._setSelected(opt)
self._set_selected(opt)
if not self.is_multiple:
return
matched = True
Expand All @@ -98,7 +94,7 @@ def select_by_index(self, index):
match = str(index)
for opt in self.options:
if opt.get_attribute("index") == match:
self._setSelected(opt)
self._set_selected(opt)
return
raise NoSuchElementException("Could not locate element with index %d" % index)

Expand All @@ -113,25 +109,25 @@ def select_by_visible_text(self, text):
throws NoSuchElementException If there is no option with specified text in SELECT
"""
xpath = ".//option[normalize-space(.) = %s]" % self._escapeString(text)
xpath = ".//option[normalize-space(.) = %s]" % self._escape_string(text)
opts = self._el.find_elements(By.XPATH, xpath)
matched = False
for opt in opts:
self._setSelected(opt)
self._set_selected(opt)
if not self.is_multiple:
return
matched = True

if len(opts) == 0 and " " in text:
subStringWithoutSpace = self._get_longest_token(text)
if subStringWithoutSpace == "":
sub_string_without_space = self._get_longest_token(text)
if sub_string_without_space == "":
candidates = self.options
else:
xpath = ".//option[contains(.,%s)]" % self._escapeString(subStringWithoutSpace)
xpath = ".//option[contains(.,%s)]" % self._escape_string(sub_string_without_space)
candidates = self._el.find_elements(By.XPATH, xpath)
for candidate in candidates:
if text == candidate.text:
self._setSelected(candidate)
self._set_selected(candidate)
if not self.is_multiple:
return
matched = True
Expand All @@ -146,7 +142,7 @@ def deselect_all(self):
if not self.is_multiple:
raise NotImplementedError("You may only deselect all options of a multi-select")
for opt in self.options:
self._unsetSelected(opt)
self._unset_selected(opt)

def deselect_by_value(self, value):
"""Deselect all options that have a value matching the argument. That is, when given "foo" this
Expand All @@ -162,10 +158,10 @@ def deselect_by_value(self, value):
if not self.is_multiple:
raise NotImplementedError("You may only deselect options of a multi-select")
matched = False
css = "option[value = %s]" % self._escapeString(value)
css = "option[value = %s]" % self._escape_string(value)
opts = self._el.find_elements(By.CSS_SELECTOR, css)
for opt in opts:
self._unsetSelected(opt)
self._unset_selected(opt)
matched = True
if not matched:
raise NoSuchElementException("Could not locate element with value: %s" % value)
Expand All @@ -183,7 +179,7 @@ def deselect_by_index(self, index):
raise NotImplementedError("You may only deselect options of a multi-select")
for opt in self.options:
if opt.get_attribute("index") == str(index):
self._unsetSelected(opt)
self._unset_selected(opt)
return
raise NoSuchElementException("Could not locate element with index %d" % index)

Expand All @@ -199,23 +195,23 @@ def deselect_by_visible_text(self, text):
if not self.is_multiple:
raise NotImplementedError("You may only deselect options of a multi-select")
matched = False
xpath = ".//option[normalize-space(.) = %s]" % self._escapeString(text)
xpath = ".//option[normalize-space(.) = %s]" % self._escape_string(text)
opts = self._el.find_elements(By.XPATH, xpath)
for opt in opts:
self._unsetSelected(opt)
self._unset_selected(opt)
matched = True
if not matched:
raise NoSuchElementException("Could not locate element with visible text: %s" % text)

def _setSelected(self, option):
def _set_selected(self, option) -> None:
if not option.is_selected():
option.click()

def _unsetSelected(self, option):
def _unset_selected(self, option) -> None:
if option.is_selected():
option.click()

def _escapeString(self, value):
def _escape_string(self, value: str) -> str:
if '"' in value and "'" in value:
substrings = value.split("\"")
result = ["concat("]
Expand All @@ -232,7 +228,7 @@ def _escapeString(self, value):

return "\"%s\"" % value

def _get_longest_token(self, value):
def _get_longest_token(self, value: str) -> str:
items = value.split(" ")
longest = ""
for item in items:
Expand Down

0 comments on commit 05467b6

Please sign in to comment.