1、文本框(text field or textarea)
element.sendKeys(“test”);//在输入框中输入内容: element.clear(); //将输入框清空 element.getText(); //获取输入框的文本内容:
2、下来选择框(select)
Select select = new Select(driver.findElement(By.id("select"))); select.selectByVisibleText(“A”); select.selectByValue(“1”); select.deselectAll(); select.deselectByValue(“1”); select.deselectByVisibleText(“A”); select.getAllSelectedOptions(); select.getFirstSelectedOption();
3、单选项(Radio Button)
WebElement radio=driver.findElement(By.id("BookMode")); radio.click(); //选择某个单选项 radio.clear(); //清空某个单选项 radio.isSelected(); //判断某个单选项是否已经被选择
4、多选项(checkbox)
WebElement checkbox = driver.findElement(By.id("myCheckbox."));
checkbox.click();
checkbox.clear();
checkbox.isSelected();
checkbox.isEnabled();
5、按钮(button)
WebElement btn= driver.findElement(By.id("save")); btn.click(); //点击按钮 btn.isEnabled (); //判断按钮是否enable
6、弹出对话框(popup dialogs)
Alert alert = driver.switchTo().alert(); alert.accept(); //确定 alert.dismiss(); //取消 alert.getText(); //获取文本
6、表单(form)
Form中的元素的操作和其它的元素操作一样,对元素操作完成后对表单的提交可以: WebElement approve = driver.findElement(By.id("approve")); approve.click(); 或 approve.submit();//只适合于表单的提交
7、上传文件
WebElement adFileUpload =driver.findElement(By.id("WAP-upload")); String filePath = "C: est\uploadfile\media_ads\test.jpg"; adFileUpload.sendKeys(filePath);
8、windows和frames切换
driver.switchTo().defaultContent(); //返回到最顶层的frame/iframe driver.switchTo().frame("leftFrame"); //切换到某个frame: driver.switchTo().window("windowName"); //切换到某个window
9、超时设置
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //识别元素时的超时时间 driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS); //页面加载时的超时时间 driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS); //异步脚本的超时时间
10、调用JavaScript
1)webdriver中使用js
//1、初始化JavascriptExecutor WebDriver dr = new InternetExplorerDriver(); //定义IE浏览器 //WebDriver driver = new FirefoxDriver();定义火狐 JavascriptExecutor js = (JavascriptExecutor) driver; //2、直接传入JavaScript代码 js.executeScript("window.document.getElementById('jingshou').click()"); //3、传入webelement执行js
WebElement thelink = driver.findElement(By.xpath("//a[text()='预付卡']"));// 或者 By.xpath("//*[@id='row_prepay']/a") js.executeScript("arguments[0].click()", thelink);
// 其中arguments[0]就代表element WebElement thelink = driver.findElement(By.id("jingshou")); js.executeScript("arguments[0].onclick=function(){alert('This is my alert!');}", element)
// 其中arguments[0]就代表div,arguments[1]就代表"height:1000px" WebElement div = driver.findElemnt(By.id("myDiv")); js.executeScript("arguments[0].setAttribute('style', arguments[1])", div, "height: 1000px");
2)java中使用js
//1、初始化 ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByName("JavaScript"); //2、加载js String jsfile = "D:/test.js"; engine.eval(new FileReader(jsfile )); //3、初始invocable对象 // Invocable 接口: 允许java平台调用脚本程序中的函数或方法 Invocable inv = (Invocable) engine; //4、调用方法 //调用js文件方法 inv.invokeFunction("run1", new Object[]{"aa","bb"}); //直接在java中定义方法 engine.eval("function run(aa) {println('打印结果:www.java2s.com:'+aa); return aa}"); String aa = (String)inv.invokeFunction("run", "haoba");
11、遍历表格
List<WebElement> tables = driver.findElements(By.id("mytable")); //获取table标签元素 WebElement table = tables.get(1); List<WebElement> rows = table.findElements(By.tagName("tr")); for (WebElement row : rows) { List<WebElement> cols = row.findElements(By.tagName("td")); boolean flag = false; for (WebElement col : cols) { if (col.getText().equals("execInvestPlanQuartzTriger")) { flag = true; } if (flag && col.getText().contains("立即执行")) { Reporter.log("#############表格内容:" + col.getText()); WebElement element = col.findElements(By.tagName("span")).get(1).findElement(By.linkText("立即执行")); UIUtils.getElementByXpath(driver, "//*[@id="run_execInvestPlanQuartzTriger_0"]/a").click(); UIUtils.waitMoment(); Autoit3.runAsync("弹出框.au3"); UIUtils.waitMoment(); break; } } }