@@ -756,15 +756,50 @@ def set_attribute(self, selector, attribute, value, by=By.CSS_SELECTOR,
756
756
% (css_selector , attribute , value ))
757
757
self .execute_script (script )
758
758
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
+
759
791
def remove_attribute (self , selector , attribute , by = By .CSS_SELECTOR ,
760
792
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. """
762
795
if self .timeout_multiplier and timeout == settings .SMALL_TIMEOUT :
763
796
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 )
766
798
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
768
803
attribute = re .escape (attribute )
769
804
attribute = self .__escape_quotes_if_needed (attribute )
770
805
css_selector = self .convert_to_css_selector (selector , by = by )
0 commit comments