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)