标签:
例子:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>calc</title> 6 <script> 7 function calcResult() { 8 var num1 = document.getElementById("id1").value; 9 var calctag = document.getElementById("id2").value; 10 var num2 = document.getElementById("id3").value; 11 var result = 0; 12 13 switch (calctag) { 14 case "+": 15 result = parseInt(num1) + parseInt(num2); 16 break; 17 case "-": 18 result = parseInt(num1) - parseInt(num2); 19 break; 20 case "*": 21 result = parseInt(num1) * parseInt(num2); 22 break; 23 case "/": 24 if (parseInt(num2) == 0) { 25 alert("数字2不能为0"); 26 } else { 27 result = parseInt(num1) / parseInt(num2); 28 } 29 break; 30 default: 31 alert("......"); 32 } 33 34 document.getElementById("id5").value = result; 35 36 } 37 </script> 38 </head> 39 <body> 40 <form> 41 数字1:<input type="text" id="id1" name="num1" /> 42 <select id="id2" name="calc"> 43 <option value="+" selected="selected">加</option> 44 <option value="-">减</option> 45 <option value="*">乘</option> 46 <option value="/">除</option> 47 </select> 48 数字2:<input type="text" id="id3" name="num2" /> 49 <input type="button" name="is" id="id4" value="=" onclick="calcResult()" /> 50 结果:<input type="text" id="id5" name="result" /> 51 </form> 52 </body> 53 </html>
代码:
1 package com.test.select; 2 3 import java.util.Iterator; 4 import java.util.List; 5 6 import org.openqa.selenium.By; 7 import org.openqa.selenium.WebDriver; 8 import org.openqa.selenium.WebElement; 9 import org.openqa.selenium.firefox.FirefoxDriver; 10 import org.openqa.selenium.support.ui.Select; 11 12 public class SelectTest { 13 14 15 16 public static void main(String[] args) { 17 WebDriver driver = new FirefoxDriver(); 18 driver.get("file:///D:/10-selenium/workspace/SeleniumTest/src/com/test/select/calc.html"); 19 driver.manage().window().maximize(); 20 21 driver.findElement(By.id("id1")).sendKeys("4"); 22 23 Select sel = new Select(driver.findElement(By.name("calc"))); 24 sel.selectByValue("/"); 25 26 driver.findElement(By.id("id3")).sendKeys("2"); 27 28 driver.findElement(By.id("id4")).click(); 29 30 System.out.println(driver.findElement(By.id("id5")).getAttribute("value")); 31 32 //////////////////////////////////////////////////////////////// 33 driver.findElement(By.id("id1")).clear(); 34 driver.findElement(By.id("id1")).sendKeys("3"); 35 36 Select sel2 = new Select(driver.findElement(By.name("calc"))); 37 sel2.selectByValue("+"); 38 39 driver.findElement(By.id("id3")).clear(); 40 driver.findElement(By.id("id3")).sendKeys("1"); 41 42 driver.findElement(By.id("id4")).click(); 43 44 System.out.println(driver.findElement(By.id("id5")).getAttribute("value")); 45 46 //////////////////////////////////////////////////////////////// 47 driver.findElement(By.id("id1")).clear(); 48 driver.findElement(By.id("id1")).sendKeys("5"); 49 50 Select sel3 = new Select(driver.findElement(By.name("calc"))); 51 sel3.selectByValue("*"); 52 53 driver.findElement(By.id("id3")).clear(); 54 driver.findElement(By.id("id3")).sendKeys("6"); 55 56 driver.findElement(By.id("id4")).click(); 57 58 System.out.println(driver.findElement(By.id("id5")).getAttribute("value")); 59 60 //////////////////////////////////////////////////////////////// 61 driver.findElement(By.id("id1")).clear(); 62 driver.findElement(By.id("id1")).sendKeys("100"); 63 64 Select sel4 = new Select(driver.findElement(By.name("calc"))); 65 sel4.selectByValue("-"); 66 67 driver.findElement(By.id("id3")).clear(); 68 driver.findElement(By.id("id3")).sendKeys("1"); 69 70 driver.findElement(By.id("id4")).click(); 71 72 System.out.println(driver.findElement(By.id("id5")).getAttribute("value")); 73 74 //////////////////////////////////////////////////////////////////////////////// 75 Select selall = new Select(driver.findElement(By.name("calc"))); 76 77 List<WebElement> lw= selall.getOptions(); 78 Iterator<WebElement> iterator = lw.iterator(); 79 while(iterator.hasNext()){ 80 System.out.println(iterator.next().getAttribute("value")); 81 } 82 83 driver.quit(); 84 85 } 86 87 88 89 }
结果:
2 4 30 99 + - * /
标签:
原文地址:http://www.cnblogs.com/moonpool/p/5769484.html