转自https://blog.csdn.net/rhy0619/article/details/79004983
启动浏览器
firefoxdriver:
System.setProperty("webdriver.firefox.bin","C:\\tools\\firefox.exe");
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("permissions.default.stylesheet", 2);//禁用css
profile.setPreference("permissions.default.image", 2);//禁用图片
profile.setPreference("dom.ipc.plugins.enabled.libflashplayer.so","false");//禁用flash
WebDriver driver = new FirefoxDriver(profile);
chromedriver:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--start-maximized");
System.setProperty("webdriver.chrome.driver","C:\\tools\\chromedriver.exe");
WebDriver driver = new ChromeDriver(chromeOptions);
phantomjsdriver:
System.setProperty("phantomjs.binary.path", "C:\\tools\\phantomjs.exe");
WebDriver driver = new PhantomJSDriver();
浏览器最大化:
driver.manage().window().maximize();
关闭浏览器:
driver.close();//关闭当前窗口
driver.quit();//关闭全部窗口
遍历窗口句柄:
String currentHandle = driver.getWindowHandle();//当前句柄
Set<String> windowHandles = driver.getWindowHandles();//全部句柄
for (String handle : windowHandles) {
if(currentHandle.equals(handle)){
continue;
}else{
driver.switchTo().window(handle);
}
}
打开页面:
driver.get("http://www.baidu.com");
driver.navigate().to("http://www.baidu.com/");
driver.navigate().forward();//向前
driver.navigate().back();//向后
driver.navigate().refresh();//刷新
获取页面的标题
driver.getTitle();
Cookie
driver.manage().deleteAllCookies();//删除cookie
driver.manage().addCookie(<Cookie>);//添加cookie
Set<Cookie> cookiesSet = driver.manage().getCookies();//获取所有cookie
for (Cookie c : cookiesSet) {
String cookies+= c.getName() + "=" + c.getValue() + ";";
}
页面元素定位
By.id:driver.findElement(By.id("id));
By.name:driver.findElement(By.name("name"));
By.xpath:driver.findElement(By.xpath("//input[@id='id']"));
By.className:driver.findElement(By.className("input_class"));
By.cssSelector:driver.findElement(By.cssSelector(".input_class"));
By.linkText:driver.findElement(By.linkText("link"));
By.partialLinkText:driver.findElement(By.partialLinkText("link"));
By.tagName:driver.findElement(By.tagName("form"));
判断元素是否显示
element.isDisplay();
获取元素属性
element.getAttribute(<attribute>); //得到元素的属性值
输入框
element.sendKeys(“test”);//在输入框中输入内容:
element.clear(); //将输入框清空
element.getText(); //获取输入框的文本内容:
下拉选择框
select.selectByVisibleText(“A”);
select.selectByValue(“1”);
select.deselectAll();
select.deselectByValue(“1”);
select.deselectByVisibleText(“A”);
select.getAllSelectedOptions();
select.getFirstSelectedOption();
单选框
radio.click();//选择某个单选项
radio.clear();//清空某个单选项
radio.isSelected();//判断某个单选项是否已经被选择
多选框
checkbox.click();
checkbox.clear();
checkbox.isSelected(); //判断多选框是否被选择
checkbox.isEnabled(); //判断多选框是否可用
按钮
btn.click();//点击按钮
btn.isEnabled ();//判断按钮是否可用
Alert对话框
alert.accept(); //点击确定
alert.dismiss(); //点击取消
alert.getText(); //获取Alert对话框的文本
表单
approve.click();
approve.submit();//只适合于表单的提交
上传文件
driver.sendKeys(filePath);
这个方法并不适用于所有上传文件的步骤,有的需要借助autoIt这个工具,试具体的Web项目而定。
Windows 和 Frames之间的切换
driver.switchTo().defaultContent(); //返回到最顶层的frame/iframe
driver.switchTo().frame("leftFrame"); //切换到某个frame:
driver.switchTo().window("windowName"); //切换到某个window
调用Java Script
JavascriptExecutor js =(JavascriptExecutor) driver;
js.executeScript("JS脚本");
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView()", element); //滚动到指定元素
((JavascriptExecutor)driver).executeScript("document.documentElement.scrollTop=" + height); //滚动到指定高度
((JavascriptExecutor)driver).executeScript("var q=document.documentElement.scrollTop=0"); //滚动到页面顶部
((JavascriptExecutor)driver).executeScript("arguments[0].click();",element); //点击隐藏元素
超时设置
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS); //识别元素时的超时时间
driver.manage().timeouts().pageLoadTimeout(10,TimeUnit.SECONDS); //页面加载时的超时时间
driver.manage().timeouts().setScriptTimeout(10,TimeUnit.SECONDS); //异步脚本的超时时间
模拟键盘
Actions action = new Actions(driver);
action.sendKeys(<Keys>).perform();
截屏
File file = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
/**
* 获取验证码
* @param driver
*/
private static String getPicCode(WebDriver driver,WebElement comment) {
try{
File screenshotAs = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage bufferedImage = ImageIO.read(screenshotAs);
Point point = comment.getLocation();
int width = comment.getSize().getWidth();
int height = comment.getSize().getHeight();
BufferedImage subimage = bufferedImage.getSubimage(point.getX(), point.getY(), width, height);
ImageIO.write(subimage, "png", screenshotAs);
File file = new File("d:\\xxx.png");
FileUtils.copyFile(screenshotAs, file);
return code;
}catch(Exception e){
driver.close();
}
return null;
}
}
//滑动
actions.dragAndDropBy(button, Integer.parseInt(param.split(",")[0])-23, Integer.parseInt(param.split(",")[1])).perform();