selenium测试(Java)--下拉框(二十一)
Posted qingxin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了selenium测试(Java)--下拉框(二十一)相关的知识,希望对你有一定的参考价值。
例子:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>calc</title> <script> 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("数字2不能为0"); } else { result = parseInt(num1) / parseInt(num2); } break; default: alert("......"); } document.getElementById("id5").value = result; } </script> </head> <body> <form> 数字1:<input type="text" id="id1" name="num1" /> <select id="id2" name="calc"> <option value="+" selected="selected">加</option> <option value="-">减</option> <option value="*">乘</option> <option value="/">除</option> </select> 数字2:<input type="text" id="id3" name="num2" /> <input type="button" name="is" id="id4" value="=" onclick="calcResult()" /> 结果:<input type="text" id="id5" name="result" /> </form> </body> </html>
代码:
package com.test.select; import java.util.Iterator; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.Select; public class SelectTest { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); 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 = new Select(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 = new Select(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 = new Select(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 = new Select(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 = new Select(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(); } }
结果:
4 99 + - * /
以上是关于selenium测试(Java)--下拉框(二十一)的主要内容,如果未能解决你的问题,请参考以下文章