The following page code, such as HTML
< HTML > < Body > < type= "text" name= "text" value= "Alone" > </ Body > </ HTML >
Use the following script to change the properties of a label
Public classSelenium { Public StaticWebdriver Jsdriver; @BeforeMethod Public voidIntidriver () {System.setproperty ("Phantomjs.binary.path", "D:\\java\\ideaworkstation\\casual\\src\\main\\resources\\driver\\phantomjs.exe"); Jsdriver=NewPhantomjsdriver (); Jsdriver.manage (). Timeouts (). Implicitlywait (5, Timeunit.seconds); Jsdriver.get ("C:\\users\\win7\\desktop\\select.html"); } @Test (Enabled=true) Public voidJavascriptexcutor () {webelement Textele=jsdriver.findelement (By.name ("text")); System.out.println (Textele.getattribute ("value"));//enter the value before the changeString jsstrtosetatt= "Arguments[0].setattribute (arguments[1],arguments[2])";//change the properties of JS((Javascriptexecutor) jsdriver). Executescript (Jsstrtosetatt, Textele, "value", "No Alnon")); System.out.println (Textele.getattribute ("value"));//enter the value before the changeString jsstrtoremoveatt= "Arguments[0].removeattribute (arguments[1],arguments[2])";//js to remove attributes((Javascriptexecutor) jsdriver). Executescript (Jsstrtoremoveatt, Textele, "value"); System.out.println (Jsdriver.getpagesource ());//page code after the output is changed }}
After executing the script, you will see the output as follows, stating that the attributes of the element have been modified
Alone
No Alnon
<input type= "text" name= "text" >
</body>
Use Javascriptexecutor to change page elements