MCPcopy Create free account
hub / github.com/seleniumbase/SeleniumBase / wait_for_attribute

Function wait_for_attribute

seleniumbase/fixtures/page_actions.py:906–992  ·  view source on GitHub ↗

Searches for the specified element attribute by the given selector. Returns the element object if the expected attribute is present and the expected attribute value is present (if specified). Raises NoSuchElementException if the element does not exist in the HTML within the spec

(
    driver,
    selector,
    attribute,
    value=None,
    by="css selector",
    timeout=settings.LARGE_TIMEOUT,
)

Source from the content-addressed store, hash-verified

904
905
906def wait_for_attribute(
907 driver,
908 selector,
909 attribute,
910 value=None,
911 by="css selector",
912 timeout=settings.LARGE_TIMEOUT,
913):
914 """
915 Searches for the specified element attribute by the given selector.
916 Returns the element object if the expected attribute is present
917 and the expected attribute value is present (if specified).
918 Raises NoSuchElementException if the element does not exist in the HTML
919 within the specified timeout.
920 Raises NoSuchAttributeException if the element exists in the HTML,
921 but the expected attribute/value is not present within the timeout.
922 @Params
923 driver - the webdriver object (required)
924 selector - the locator for identifying the page element (required)
925 attribute - the attribute that is expected for the element (required)
926 value - the attribute value that is expected (Default: None)
927 by - the type of selector being used (Default: "css selector")
928 timeout - the time to wait for the element attribute in seconds
929 @Returns
930 A web element object that contains the expected attribute/value
931 """
932 _reconnect_if_disconnected(driver)
933 element = None
934 element_present = False
935 attribute_present = False
936 found_value = None
937 start_ms = time.time() * 1000.0
938 stop_ms = start_ms + (timeout * 1000.0)
939 for x in range(int(timeout * 10)):
940 shared_utils.check_if_time_limit_exceeded()
941 try:
942 element = driver.find_element(by=by, value=selector)
943 element_present = True
944 attribute_present = False
945 found_value = element.get_attribute(attribute)
946 if found_value is not None:
947 attribute_present = True
948 else:
949 element = None
950 raise Exception()
951
952 if value is not None:
953 if found_value == value:
954 return element
955 else:
956 element = None
957 raise Exception()
958 else:
959 return element
960 except Exception:
961 now_ms = time.time() * 1000.0
962 if now_ms >= stop_ms:
963 break

Callers

nothing calls this directly

Calls 5

timeout_exceptionFunction · 0.85
find_elementMethod · 0.45
get_attributeMethod · 0.45
sleepMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…