Use Javascriptexecutor to change page elements

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.