Skip to content

Commit bcd817d

Browse files
committed
Add self.is_attribute_present(selector, attribute, value)
1 parent c6f70b7 commit bcd817d

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,18 @@ def is_text_visible(self, text, selector="html", by=By.CSS_SELECTOR):
776776
selector, by = self.__recalculate_selector(selector, by)
777777
return page_actions.is_text_visible(self.driver, text, selector, by)
778778

779+
def is_attribute_present(
780+
self, selector, attribute, value=None, by=By.CSS_SELECTOR
781+
):
782+
"""Returns True if the element attribute/value is found.
783+
If the value is not specified, the attribute only needs to exist."""
784+
self.wait_for_ready_state_complete()
785+
time.sleep(0.01)
786+
selector, by = self.__recalculate_selector(selector, by)
787+
return page_actions.is_attribute_present(
788+
self.driver, selector, attribute, value, by
789+
)
790+
779791
def is_link_text_visible(self, link_text):
780792
self.wait_for_ready_state_complete()
781793
time.sleep(0.01)

seleniumbase/fixtures/page_actions.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,37 @@ def is_text_visible(driver, text, selector, by=By.CSS_SELECTOR):
106106
return False
107107

108108

109+
def is_attribute_present(
110+
driver, selector, attribute, value=None, by=By.CSS_SELECTOR
111+
):
112+
"""
113+
Returns whether the specified attribute is present in the given selector.
114+
@Params
115+
driver - the webdriver object (required)
116+
selector - the locator for identifying the page element (required)
117+
attribute - the attribute that is expected for the element (required)
118+
value - the attribute value that is expected (Default: None)
119+
by - the type of selector being used (Default: By.CSS_SELECTOR)
120+
@Returns
121+
Boolean (is attribute present)
122+
"""
123+
try:
124+
element = driver.find_element(by=by, value=selector)
125+
found_value = element.get_attribute(attribute)
126+
if found_value is None:
127+
raise Exception()
128+
129+
if value is not None:
130+
if found_value == value:
131+
return True
132+
else:
133+
raise Exception()
134+
else:
135+
return True
136+
except Exception:
137+
return False
138+
139+
109140
def hover_on_element(driver, selector, by=By.CSS_SELECTOR):
110141
"""
111142
Fires the hover event for the specified element by the given selector.

0 commit comments

Comments
 (0)