Importjava.util.ArrayList;ImportJava.util.concurrent.TimeUnit;Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.JavascriptExecutor;ImportOrg.openqa.selenium.WebDriver;Importorg.openqa.selenium.WebElement;ImportOrg.openqa.selenium.firefox.FirefoxDriver;ImportOrg.testng.annotations.AfterClass;ImportOrg.testng.annotations.BeforeClass;Importorg.testng.annotations.Test; Public classTestbaidu { Public StaticWebdriver Driver; /** @BeforeClass
* public void SetUp () {driver = new firefoxdriver (); * Driver.manage (). window (). Maximize (); } * * @AfterClass
* public void TearDown () {driver.close (); Driver.quit ();} * * @Test
* public void process () {* Driver.navigate (). to ("http://www.baidu.com"); * Driver.findelement (By.id ("kw")); Javascriptexecutor j = * (javascriptexecutor) driver; * J.executescript ("document.getElementById (\" kw\ "). value=\" Test \ ""); } */ Public Static voidMain (string[] args)throwsinterruptedexception {//you may need to set the path of FirefoxDriver =NewFirefoxdriver (); Try{driver.manage (). window (). Maximize (); Driver.get ("Http://www.baidu.com"); //type search keywords with webdriver//driver.findelement (by.id ("kw")). SendKeys ("Yeetrack"); //use JS code to type search keywords( (Javascriptexecutor) driver). Executescript ("document.getElementById (\" kw\ "). value=\" Test \ ""); //use JS code to remove keywordsString keyword =(String) ((javascriptexecutor) driver). Executescript ("var input = document.getElementById (\" kw\ "). Value;return input"); SYSTEM.OUT.PRINTLN (keyword); Driver.findelement (By.id ("Su") . Click (); TimeUnit.SECONDS.sleep (5); } Catch(Exception e) {e.printstacktrace (); } finally{driver.quit (); } }}
Selenium Execution JS