Skip to content

Commit 1a4f4ff

Browse files
committed
Add methods: get_property() and get_text_content()
1 parent 0d1de72 commit 1a4f4ff

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

help_docs/method_summary.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ self.remove_attribute(selector, attribute, by=By.CSS_SELECTOR, timeout=None)
110110

111111
self.remove_attributes(selector, attribute, by=By.CSS_SELECTOR)
112112

113+
self.get_property(selector, property, by=By.CSS_SELECTOR, timeout=None)
114+
115+
self.get_text_content(selector, by=By.CSS_SELECTOR, timeout=None)
116+
113117
self.get_property_value(selector, property, by=By.CSS_SELECTOR, timeout=None)
114118

115119
self.get_image_url(selector, by=By.CSS_SELECTOR, timeout=None)

seleniumbase/fixtures/base_case.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,50 @@ def remove_attributes(self, selector, attribute, by=By.CSS_SELECTOR):
14031403
except Exception:
14041404
pass
14051405

1406+
def get_property(
1407+
self, selector, property, by=By.CSS_SELECTOR, timeout=None
1408+
):
1409+
"""Returns the property value of an element.
1410+
This is not the same as self.get_property_value(), which returns
1411+
the value of an element's computed style using a different algorithm.
1412+
If no result is found, an empty string (instead of None) is returned.
1413+
Example:
1414+
html_text = self.get_property(SELECTOR, "textContent")
1415+
"""
1416+
self.__check_scope()
1417+
if not timeout:
1418+
timeout = settings.SMALL_TIMEOUT
1419+
if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:
1420+
timeout = self.__get_new_timeout(timeout)
1421+
selector, by = self.__recalculate_selector(selector, by)
1422+
self.wait_for_ready_state_complete()
1423+
time.sleep(0.01)
1424+
element = page_actions.wait_for_element_present(
1425+
self.driver, selector, by, timeout
1426+
)
1427+
try:
1428+
property_value = element.get_property(property)
1429+
except (StaleElementReferenceException, ENI_Exception):
1430+
self.wait_for_ready_state_complete()
1431+
time.sleep(0.14)
1432+
element = page_actions.wait_for_element_present(
1433+
self.driver, selector, by, timeout
1434+
)
1435+
property_value = element.get_property(property)
1436+
if not property_value:
1437+
return ""
1438+
return property_value
1439+
1440+
def get_text_content(self, selector, by=By.CSS_SELECTOR, timeout=None):
1441+
"""Returns the text that appears in the HTML for an element.
1442+
This is different from "self.get_text(selector, by=By.CSS_SELECTOR)"
1443+
because that only returns the visible text on a page for an element,
1444+
rather than the HTML text that's being returned from this method."""
1445+
self.__check_scope()
1446+
return self.get_property(
1447+
selector, property="textContent", by=by, timeout=timeout
1448+
)
1449+
14061450
def get_property_value(
14071451
self, selector, property, by=By.CSS_SELECTOR, timeout=None
14081452
):

0 commit comments

Comments
 (0)