Example:
<! DOCTYPE html>function Calcresult () {var num1= document.getElementById ("Id1"). Value; var Calctag= document.getElementById ("Id2"). Value; var num2= document.getElementById ("ID3"). Value; var result= 0; Switch(Calctag) { Case"+": Result= parseint (NUM1) +parseint (num2); Break; Case"-": Result= parseint (NUM1)-parseint (num2); Break; Case"*": Result= parseint (NUM1) *parseint (num2); Break; Case"/": if(parseint (num2) = = 0) {alert ("Number 2 cannot be 0"); } Else{result= parseint (NUM1)/parseint (num2); } Break; default: Alert ("......"); } document.getElementById ("ID5"). Value =result; }</script>Number 1:<input type= "text" id= "ID1" name= "Num1"/> <select id= "Id2" name= "Calc" > <option value= " + "selected=" selected "> Plus </option> <option value="-"> Minus </option> <option value = "*" > Multiply </option> <option value= "/" > except </option> </select>Number 2:<input type= "text" id= "ID3" name= "num2"/> <input type= "button" Name= "is" id= "Id4" value= "=" onclick= "cal Cresult () "/>Results:<input type= "text" id= "ID5" name= "result"/> </form></body>Code:
PackageCom.test.select;ImportJava.util.Iterator;Importjava.util.List;Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.WebDriver;Importorg.openqa.selenium.WebElement;ImportOrg.openqa.selenium.firefox.FirefoxDriver;ImportOrg.openqa.selenium.support.ui.Select; Public classSelecttest { Public Static voidMain (string[] args) {Webdriver driver=NewFirefoxdriver (); Driver.get ("File:///D:/10-selenium/workspace/SeleniumTest/src/com/test/select/calc.html"); Driver.manage (). window (). Maximize (); Driver.findelement (By.id ("Id1")). SendKeys ("4")); Select sel=NewSelect (Driver.findelement (By.name ("Calc")))); Sel.selectbyvalue ("/"); Driver.findelement (By.id ("ID3")). SendKeys ("2")); Driver.findelement (By.id ("Id4") . Click (); System.out.println (Driver.findelement (By.id ("ID5")). GetAttribute ("value"))); ////////////////////////////////////////////////////////////////Driver.findelement (By.id ("Id1") . Clear (); Driver.findelement (By.id ("Id1")). SendKeys ("3")); Select Sel2=NewSelect (Driver.findelement (By.name ("Calc")))); Sel2.selectbyvalue ("+"); Driver.findelement (By.id ("ID3") . Clear (); Driver.findelement (By.id ("ID3")). SendKeys ("1")); Driver.findelement (By.id ("Id4") . Click (); System.out.println (Driver.findelement (By.id ("ID5")). GetAttribute ("value"))); ////////////////////////////////////////////////////////////////Driver.findelement (By.id ("Id1") . Clear (); Driver.findelement (By.id ("Id1")). SendKeys ("5")); Select Sel3=NewSelect (Driver.findelement (By.name ("Calc")))); Sel3.selectbyvalue ("*"); Driver.findelement (By.id ("ID3") . Clear (); Driver.findelement (By.id ("ID3")). SendKeys ("6")); Driver.findelement (By.id ("Id4") . Click (); System.out.println (Driver.findelement (By.id ("ID5")). GetAttribute ("value"))); ////////////////////////////////////////////////////////////////Driver.findelement (By.id ("Id1") . Clear (); Driver.findelement (By.id ("Id1")). SendKeys ("100")); Select Sel4=NewSelect (Driver.findelement (By.name ("Calc")))); Sel4.selectbyvalue ("-"); Driver.findelement (By.id ("ID3") . Clear (); Driver.findelement (By.id ("ID3")). SendKeys ("1")); Driver.findelement (By.id ("Id4") . Click (); System.out.println (Driver.findelement (By.id ("ID5")). GetAttribute ("value"))); ////////////////////////////////////////////////////////////////////////////////Select Selall =NewSelect (Driver.findelement (By.name ("Calc")))); List<WebElement> lw=selall.getoptions (); Iterator<WebElement> iterator =Lw.iterator (); while(Iterator.hasnext ()) {System.out.println (Iterator.next (). GetAttribute ("Value")); } driver.quit (); } }
Results:
499+-*/
Selenium Test (Java)--drop-down box (21)