Selenium Test (Java)--Execute JS (18)

Source: Internet
Author: User

1. Operating the scroll bar

 Packagecom.test.js;Importorg.openqa.selenium.By;Importorg.openqa.selenium.Dimension;ImportOrg.openqa.selenium.JavascriptExecutor;ImportOrg.openqa.selenium.WebDriver;ImportOrg.openqa.selenium.firefox.FirefoxDriver; Public classWindowscroll { Public Static voidMain (string[] args) {Webdriver driver=NewFirefoxdriver (); Driver.get ("Http://www.baidu.com"); Driver.manage (). window (). SetSize (NewDimension (600, 600)); WaitTime (3000); Driver.findelement (By.cssselector ("#kw")). SendKeys ("Selenium")); Driver.findelement (By.cssselector ("#su") . Click (); WaitTime (3000); String JS= "Window.scrollto (100,450);";        ((Javascriptexecutor) driver). Executescript (JS); WaitTime (5000);    Driver.quit (); }    Static  Public voidWaitTime (intTime ) {        Try{thread.sleep (time); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }}

2. Enter content in textarea

 Packagecom.test.js;Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.JavascriptExecutor;ImportOrg.openqa.selenium.WebDriver;ImportOrg.openqa.selenium.firefox.FirefoxDriver; Public classTextareainput { Public Static voidMain (string[] args) {Webdriver driver=NewFirefoxdriver (); Driver.get ("File:///D:/10-selenium/workspace/SeleniumTest/src/com/test/js/textarea.html");        Driver.manage (). window (). Maximize (); Driver.findelement (By.cssselector ("#id"). SendKeys ("Input text----")); //use JS to enter contentWaitTime (5000); String text= "Input by JS"; String JS= "var sum = document.getElementById (' id '); Sum.value= ' + text + '; ';        System.out.println (JS);    ((Javascriptexecutor) driver). Executescript (JS); }    Static  Public voidWaitTime (intTime ) {        Try{thread.sleep (time); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }}

Reference:

Http://www.cnblogs.com/tobecrazy/p/4817946.html

Selenium Test (Java)--Execute JS (18)

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.