Skip to content

Commit 39e924a

Browse files
committed
Add set_attributes() / set_attribute_all()
1 parent b5f743b commit 39e924a

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -756,15 +756,50 @@ def set_attribute(self, selector, attribute, value, by=By.CSS_SELECTOR,
756756
% (css_selector, attribute, value))
757757
self.execute_script(script)
758758

759+
def set_attributes(self, selector, attribute, value, by=By.CSS_SELECTOR):
760+
""" This method uses JavaScript to set/update a common attribute.
761+
All matching selectors from querySelectorAll() are used.
762+
Example => (Make all links on a website redirect to Google):
763+
self.set_attributes("a", "href", "https://google.com") """
764+
selector, by = self.__recalculate_selector(selector, by)
765+
attribute = re.escape(attribute)
766+
attribute = self.__escape_quotes_if_needed(attribute)
767+
value = re.escape(value)
768+
value = self.__escape_quotes_if_needed(value)
769+
css_selector = self.convert_to_css_selector(selector, by=by)
770+
css_selector = re.escape(css_selector)
771+
css_selector = self.__escape_quotes_if_needed(css_selector)
772+
script = ("""var $elements = document.querySelectorAll('%s');
773+
var index = 0, length = $elements.length;
774+
for(; index < length; index++){
775+
$elements[index].setAttribute('%s','%s');}"""
776+
% (css_selector, attribute, value))
777+
try:
778+
self.execute_script(script)
779+
except Exception:
780+
pass
781+
782+
def set_attribute_all(self, selector, attribute, value,
783+
by=By.CSS_SELECTOR):
784+
""" Same as set_attributes(), but using querySelectorAll naming scheme.
785+
This method uses JavaScript to set/update a common attribute.
786+
All matching selectors from querySelectorAll() are used.
787+
Example => (Make all links on a website redirect to Google):
788+
self.set_attribute_all("a", "href", "https://google.com") """
789+
self.set_attributes(selector, attribute, value, by=by)
790+
759791
def remove_attribute(self, selector, attribute, by=By.CSS_SELECTOR,
760792
timeout=settings.SMALL_TIMEOUT):
761-
""" This method uses JavaScript to remove an attribute. """
793+
""" This method uses JavaScript to remove an attribute.
794+
Only the first matching selector from querySelector() is used. """
762795
if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:
763796
timeout = self.__get_new_timeout(timeout)
764-
if page_utils.is_xpath_selector(selector):
765-
by = By.XPATH
797+
selector, by = self.__recalculate_selector(selector, by)
766798
if self.is_element_visible(selector, by=by):
767-
self.scroll_to(selector, by=by, timeout=timeout)
799+
try:
800+
self.scroll_to(selector, by=by, timeout=timeout)
801+
except Exception:
802+
pass
768803
attribute = re.escape(attribute)
769804
attribute = self.__escape_quotes_if_needed(attribute)
770805
css_selector = self.convert_to_css_selector(selector, by=by)

0 commit comments

Comments
 (0)