Foreplay
Making web automation, we sometimes need to get property of an element, sometimes you need to add, and sometimes need to be removed, this time it should be operated by js
Real
from Selenium Import the webdriver Import the unittest DEF the addAttribute (Driver, elementobj, attributeName, value): '' ' method of packaging add new attributes to the page tab call JS to the page tab add new properties, arguments [0] ~ arguments [ 2] , respectively, will be replace with the latter element, attributeName and value parameters JS code syntax to add new attributes: element.attributeName = value such as = input.name 'Test' '' ' driver.execute_script ( " arguments [0] =% S arguments. [. 1] " % attributeName, elementobj, value) DEF : the setAttribute (Driver, elementobj, attributeName, value) '' ' a method of encapsulating an attribute value setting page object call to modify an attribute value JS code page elements, arguments [0] ~ arguments [ 1] , respectively, may be replaced with the latter element, attributeName and value parameters ' '' driver.execute_script ( " arguments [0] .setAttribute (arguments [. 1 ], arguments [2]) " , elementobj, attributeName, value) DEF the getAttribute (elementobj, attributeName): # package acquired page object attribute values methods return elementobj.get_attribute (attributeName) DEF removeAttribute (Driver, elementobj, attributeName): ' ' package delete page attribute method call delete page elements JS code specified attribute, arguments [0] ~ arguments [ 1] , respectively, may be replaced with the latter element, attributeName parameter ' '' driver.execute_script ( " arguments [0] .removeAttribute (arguments [. 1]) " , elementobj, attributeName) class TestDemo(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() def test_dataPicker(self): url = "D:\PycharmProjects\zouzou\dom.html" self.driver.get(url) element = self.driver.find_element_by_xpath('//input') # 向页面文本框input标签中添加新属性name='search' addAttribute(self.driver, element, 'name', ' Search ' ) #After adding new properties, look at the new property value Print ( ' the new property value added S =% "% S" ' % ( " name " , getAttribute (Element, " name " ))) Print ( ' change the contents of the text box value before the value: ' , the getAttribute (Element, ' value ' )) # change the value of the attribute value, "which is a value change" the setAttribute (self.driver, Element, ' value ' , ' which is changed after value ' ) Print ( ''Change the value of the value: ' , getAttribute (Element, value ' )) # review the changes before the input elements on the page in size attribute value Print ( ' change before the size attribute value: ' , getAttribute (Element, ' size ' )) # change the input of the property value 20 setAttribute (self.driver, element, ' size ' , 20 ) Print ( ' after changing the size of the property value: ' , getAttribute (element, ' size ' )) # View delete input page elements value attribute value before Print ( ' delete the value before the value of the text box: ' , The getAttribute (Element, ' value ' )) # delete the attribute value removeAttribute (self.driver, Element, ' value ' ) Print ( ' value of the text box value after a deletion: ' , the getAttribute (Element, ' value ' )) IF the __name__ == ' __main__ ' : unittest.main ()