This method uses JavaScript to get the value of an attribute. If the attribute doesn't exist or isn't found, an exception will get raised if hard_fail is True (otherwise None is returned).
(
self,
selector,
attribute,
by="css selector",
timeout=None,
hard_fail=True,
)
| 1924 | return element_text |
| 1925 | |
| 1926 | def get_attribute( |
| 1927 | self, |
| 1928 | selector, |
| 1929 | attribute, |
| 1930 | by="css selector", |
| 1931 | timeout=None, |
| 1932 | hard_fail=True, |
| 1933 | ): |
| 1934 | """This method uses JavaScript to get the value of an attribute. |
| 1935 | If the attribute doesn't exist or isn't found, an exception will |
| 1936 | get raised if hard_fail is True (otherwise None is returned).""" |
| 1937 | self.__check_scope() |
| 1938 | if not timeout: |
| 1939 | timeout = settings.LARGE_TIMEOUT |
| 1940 | if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT: |
| 1941 | timeout = self.__get_new_timeout(timeout) |
| 1942 | selector, by = self.__recalculate_selector(selector, by) |
| 1943 | if self.__is_cdp_swap_needed(): |
| 1944 | if hard_fail: |
| 1945 | return self.cdp.get_element_attribute(selector, attribute) |
| 1946 | else: |
| 1947 | return self.cdp.get_attribute(selector, attribute) |
| 1948 | self.wait_for_ready_state_complete() |
| 1949 | time.sleep(0.01) |
| 1950 | if self.__is_shadow_selector(selector): |
| 1951 | return self.__get_shadow_attribute( |
| 1952 | selector, attribute, timeout=timeout |
| 1953 | ) |
| 1954 | element = page_actions.wait_for_element_present( |
| 1955 | self.driver, selector, by, timeout |
| 1956 | ) |
| 1957 | try: |
| 1958 | attribute_value = element.get_attribute(attribute) |
| 1959 | except (Stale_Exception, ENI_Exception, TimeoutException): |
| 1960 | self.wait_for_ready_state_complete() |
| 1961 | time.sleep(0.14) |
| 1962 | element = page_actions.wait_for_element_present( |
| 1963 | self.driver, selector, by, timeout |
| 1964 | ) |
| 1965 | attribute_value = element.get_attribute(attribute) |
| 1966 | if attribute_value is not None: |
| 1967 | return attribute_value |
| 1968 | else: |
| 1969 | if hard_fail: |
| 1970 | raise Exception( |
| 1971 | "Element {%s} has no attribute {%s}!" |
| 1972 | % (selector, attribute) |
| 1973 | ) |
| 1974 | else: |
| 1975 | return None |
| 1976 | |
| 1977 | def set_attribute( |
| 1978 | self, |
no test coverage detected