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

Method set_attributes

seleniumbase/undetected/cdp_driver/tab.py:1114–1140  ·  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)

Source from the content-addressed store, hash-verified

1112 return item
1113
1114 async def set_attributes(self, selector, attribute, value):
1115 """This method uses JavaScript to set/update a common attribute.
1116 All matching selectors from querySelectorAll() are used.
1117 Example => (Make all links on a website redirect to Google):
1118 self.set_attributes("a", "href", "https://google.com")"""
1119 attribute = re.escape(attribute)
1120 attribute = js_utils.escape_quotes_if_needed(attribute)
1121 value = re.escape(value)
1122 value = js_utils.escape_quotes_if_needed(value)
1123 if selector.startswith(("/", "./", "(")):
1124 with suppress(Exception):
1125 selector = js_utils.convert_to_css_selector(selector, "xpath")
1126 css_selector = selector
1127 css_selector = re.escape(css_selector) # Add "\\" to special chars
1128 css_selector = js_utils.escape_quotes_if_needed(css_selector)
1129 js_code = (
1130 """var $elements = document.querySelectorAll('%s');
1131 var index = 0, length = $elements.length;
1132 for(; index < length; index++){
1133 $elements[index].setAttribute('%s','%s');}""" % (
1134 css_selector,
1135 attribute,
1136 value,
1137 )
1138 )
1139 with suppress(Exception):
1140 await self.evaluate(js_code)
1141
1142 async def internalize_links(self):
1143 """All `target="_blank"` links become `target="_self"`.

Callers 2

internalize_linksMethod · 0.95
solve_captchaMethod · 0.95

Calls 2

evaluateMethod · 0.95

Tested by

no test coverage detected