1.切换frame
通过xpath路径切换
driver.switchTo().frame(driver.findElement(By.xpath("//*[@id="tab"]")));
2.处理下拉框
WebElement selectEle = dr.findElement(By.xpath("//*[@id=\"ocStatus\"]"));
Select select = new Select(selectEle);
select.selectByIndex(1);
简单封装一下
public static void selectIndex(WebDriver dr,String xpath,int index){
WebElement selectEle = dr.findElement(By.xpath(xpath));
Select select = new Select(selectEle);
select.selectByIndex(index);
}
public static void selectName(WebDriver dr,String xpath,String name){
WebElement selectEle = dr.findElement(By.xpath(xpath));
Select select = new Select(selectEle);
select.selectByVisibleText(name);
}
public static void selectValue(WebDriver dr,String xpath,String value){
WebElement selectEle = dr.findElement(By.xpath(xpath));
Select select = new Select(selectEle);
select.selectByValue(value);
}
//使用
SimpleMeth0d.selectIndex(dr,"//*[@id=\"ocStatus\"]",1);
3.各类等待机制
WebDriverWait wait = new WebDriverWait(driver,5);
//标题是不是“百度一下,你就知道”
wait.until(ExpectedConditions.titleIs("百度一下,你就知道"));
//标题是不是包含“百度一下”
wait.until(ExpectedConditions.titleContains("百度一下"));
//判断该元素是否被加载在DOM中,并不代表该元素一定可见
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='kw']")));
//判断元素(定位后)是否可见
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//*[@id='kw']"))));
//判断元素是否可见(非隐藏,并且元素的宽和高都不等以0)
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='kw']")));
//只要存在一个就是true
ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//*[@id='kw']"));
//元素中的text是否包含参数中的的字符串(元素要存在,否则会抛NoSuch异常)
ExpectedConditions.textToBePresentInElementLocated(By.xpath("//*[@id='kw']"), "百度一下");
//元素的value属性中是否包含参数中的字符串
ExpectedConditions.textToBePresentInElementValue(By.xpath("//*[@id='kw']"), "***");
//判断该表单是否可以切过去,可以就切过去并返回true,否则放回false
ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("**"));
//判断某个元素是否不存在于DOM或不可见
ExpectedConditions.invisibilityOfElementLocated(By.xpath("//*[@id='kw']"));
//判断元素是否可以点击
ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='kw']"));
//等到一个元素从DOM中移除
ExpectedConditions.stalenessOf(driver.findElement(By.xpath("//*[@id='kw']")));
//判断某个元素是否被选中,一般用在下拉列表
ExpectedConditions.elementToBeSelected(By.xpath("//*[@id='kw']"));
//判断某个元素的选中状态是否符合预期
ExpectedConditions.elementSelectionStateToBe(By.xpath("//*[@id='kw']"), true);
//判断某个元素(已定位)的选中状态是否符合预期
ExpectedConditions.elementSelectionStateToBe(driver.findElement(By.xpath("//*[@id='kw']")), false);
//判断页面中是否存在alert
wait.until(ExpectedConditions.alertIsPresent());
4.执行js脚本
executeScript 内部可以编写任意的可执行js脚本。
JavascriptExecutor js = (JavascriptExecutor) dr;
js.executeScript("document.getElementById(\"menuClar\").click()");
5.使用AutoIT上传文件
通过AutoIt第三方软件生成自动上传文件的可执行文件upload.exe,使用java执行这个文件,达到自动化上传文件的目的。文件生成方法参考https://blog.csdn.net/qq_45731111/article/details/104980174
//实现文件上传。通过Runtime的静态方法获取Runtime对象
Runtime runtime = Runtime.getRuntime();
//通过Runtime对象调用exe方法
runtime.exec("D:\\my_field\\my_git\\javaUI\\Upload.exe"+" "+"C:\\Users\\Desktop\\my_folder\\images\\9.75.pdf");
6.对于非输入框,使用模拟按键盘输入内容
文本输入框中输入的内容为this is a test content
//光标定位到非input输入框
dr.findElements(By.xpath("//*[@id=\"cke_editor1\"]"));
//模拟按键盘输入内容
Actions action = new Actions(dr);
action.sendKeys(Keys.SHIFT+"this is a test content").perform();