Skip to content

Commit e1264ab

Browse files
committed
Add method: get_property_value()
1 parent c1c1ecb commit e1264ab

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

help_docs/method_summary.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ self.maximize_window()
8585

8686
self.activate_jquery()
8787

88+
self.get_property_value(selector, property, by=By.CSS_SELECTOR,
89+
timeout=settings.SMALL_TIMEOUT)
90+
8891
self.bring_to_front(selector, by=By.CSS_SELECTOR)
8992

9093
self.highlight(selector, by=By.CSS_SELECTOR, loops=4, scroll=True)

seleniumbase/fixtures/base_case.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,40 @@ def activate_jquery(self):
706706
# Since jQuery still isn't activating, give up and raise an exception
707707
raise Exception("Exception: WebDriver could not activate jQuery!")
708708

709+
def get_property_value(self, selector, property, by=By.CSS_SELECTOR,
710+
timeout=settings.SMALL_TIMEOUT):
711+
""" Returns the property value of a page element's computed style.
712+
Example:
713+
opacity = self.get_property_value("html body a", "opacity")
714+
self.assertTrue(float(opacity) > 0, "Element not visible!") """
715+
if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:
716+
timeout = self._get_new_timeout(timeout)
717+
if page_utils.is_xpath_selector(selector):
718+
by = By.XPATH
719+
if page_utils.is_link_text_selector(selector):
720+
selector = page_utils.get_link_text_from_selector(selector)
721+
by = By.LINK_TEXT
722+
self.wait_for_ready_state_complete()
723+
page_actions.wait_for_element_present(
724+
self.driver, selector, by, timeout)
725+
try:
726+
selector = self.convert_to_css_selector(selector, by=by)
727+
except Exception:
728+
# Don't run action if can't convert to CSS_Selector for JavaScript
729+
raise Exception(
730+
"Exception: Could not convert {%s}(by=%s) to CSS_SELECTOR!" % (
731+
selector, by))
732+
selector = self.jq_format(selector)
733+
script = ("""var $elm = document.querySelector('%s');
734+
$val = window.getComputedStyle($elm).getPropertyValue('%s');
735+
return $val;"""
736+
% (selector, property))
737+
value = self.execute_script(script)
738+
if value is not None:
739+
return value
740+
else:
741+
return "" # Return an empty string if the property doesn't exist
742+
709743
def bring_to_front(self, selector, by=By.CSS_SELECTOR):
710744
""" Updates the Z-index of a page element to bring it into view.
711745
Useful when getting a WebDriverException, such as the one below:

0 commit comments

Comments
 (0)