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

Method set_attributes

seleniumbase/fixtures/base_case.py:2019–2054  ·  view source on GitHub ↗

This method uses JavaScript to set/update a common attribute. All matching selectors from querySelectorAll() are used. Example => (Make all links on a website redirect to Google): self.set_attributes("a", "href", "https://google.com")

(self, selector, attribute, value, by="css selector")

Source from the content-addressed store, hash-verified

2017 self.__extra_actions.append(action)
2018
2019 def set_attributes(self, selector, attribute, value, by="css selector"):
2020 """This method uses JavaScript to set/update a common attribute.
2021 All matching selectors from querySelectorAll() are used.
2022 Example => (Make all links on a website redirect to Google):
2023 self.set_attributes("a", "href", "https://google.com")"""
2024 self.__check_scope()
2025 selector, by = self.__recalculate_selector(selector, by)
2026 if self.__is_cdp_swap_needed():
2027 self.cdp.set_attributes(selector, attribute, value)
2028 return
2029 original_attribute = attribute
2030 original_value = value
2031 attribute = re.escape(attribute)
2032 attribute = self.__escape_quotes_if_needed(attribute)
2033 value = re.escape(value)
2034 value = self.__escape_quotes_if_needed(value)
2035 css_selector = self.convert_to_css_selector(selector, by=by)
2036 css_selector = re.escape(css_selector) # Add "\\" to special chars
2037 css_selector = self.__escape_quotes_if_needed(css_selector)
2038 script = """var $elements = document.querySelectorAll('%s');
2039 var index = 0, length = $elements.length;
2040 for(; index < length; index++){
2041 $elements[index].setAttribute('%s','%s');}""" % (
2042 css_selector,
2043 attribute,
2044 value,
2045 )
2046 with suppress(Exception):
2047 self.execute_script(script)
2048 if self.recorder_mode and self.__current_url_is_recordable():
2049 if self.get_session_storage_item("pause_recorder") == "no":
2050 time_stamp = self.execute_script("return Date.now();")
2051 origin = self.get_origin()
2052 sele_attr_val = [selector, original_attribute, original_value]
2053 action = ["s_ats", sele_attr_val, origin, time_stamp]
2054 self.__extra_actions.append(action)
2055
2056 def set_attribute_all(self, selector, attribute, value, by="css selector"):
2057 """Same as set_attributes(), but using querySelectorAll naming scheme.

Callers 2

set_attribute_allMethod · 0.95
internalize_linksMethod · 0.95

Calls 9

__check_scopeMethod · 0.95
__is_cdp_swap_neededMethod · 0.95
execute_scriptMethod · 0.95
get_originMethod · 0.95

Tested by

no test coverage detected