1)浏览器最大化
WebDriver webDriver = new ChromeDriver();
webDriver.manage().window().maximize();
2)设置浏览器大小
private static void Test01() throws InterruptedException {
//创建一个驱动
WebDriver webDriver = new ChromeDriver();
//打开百度首页
webDriver.get("https://www.baidu.com");
//设置浏览器大小200*200
webDriver.manage().window().setSize(new Dimension(200,200));
//校验百度搜索输入框存不存在
WebElement webElement=webDriver.findElement(By.cssSelector("#kw"));
try {
sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if(webElement==null){
System.out.println("测试不通过");
}else {
System.out.println("测试通过");
}
}
3)浏览器前进,后退
private static void Test03() throws InterruptedException {
//创建一个浏览器驱动
WebDriver webDriver = new ChromeDriver();
//打开百度首页
webDriver.get("https://www.baidu.com");
//输入“张三”
WebElement search_input = webDriver.findElement(By.id("kw"));
search_input.sendKeys("张三");
try {
sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
//点击百度一下按钮
WebElement baiduButton = webDriver.findElement(By.id("su"));
baiduButton.click();
try {
sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
//浏览器前进
webDriver.navigate().forward();
sleep(3000);
//获取当前页面url
String cur_url= webDriver.getCurrentUrl();
if(cur_url.equals("https://www.baidu.com")){
System.out.println("测试不通过");
}else {
System.out.println("测试通过");
}
}
4)操作浏览器滚动条
private static void Test04() throws InterruptedException {
//创建一个驱动
WebDriver webDriver = new ChromeDriver();
//打开百度首页
webDriver.get("https://www.baidu.com");
//输入“泰勒斯威夫特”
WebElement search_input= webDriver.findElement(By.id("kw"));
search_input.sendKeys("泰勒斯威夫特");
//点击百度一下
WebElement baiduButton = webDriver.findElement(By.id("su"));
baiduButton.click();
//等待3秒
try {
sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
//浏览器滚动条滑到最下面
((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=10000");
//强制类型转换,通过selenium执行js
//等待3秒
sleep(3000);
//浏览器关闭
webDriver.quit();
}