文章目录
WebDriver基础篇 -常见的页面元素操作
操作测试对象
click:点击
senKeys:在对象上进行吗,进行某些输入
clear:清除
下拉框处理
定位下拉框的三种方法
//1、利用 Index
s1.selectByIndex(3);
//2、利用 显示的值
s1.selectByVisibleText(“宝马”);
//3、利用真实的value值
s1.selectByValue(“QQ”);
上传下载
//基于Chrome浏览器
System.setProperty("webdriver.chrome.driver", "驱动路径");
WebDriver Demo10_driver=new ChromeDriver();
Demo10_driver.manage().window().maximize();//浏览器最大化
Thread.sleep(3000);
Demo10_driver.get("html/URL 路径");
WebElement upload =Demo10_driver.findElement(By.name("test"));
Thread.sleep(1500);
upload.sendKeys("d:/xxxxxxxxx.jpg");//路径
Thread.sleep(3000);
Demo10_driver.quit();
处理alert
alert的 三种形态
1. Alert (常见)
WebElement clickbtn = Demo11_alert_driver.findElement(By.xpath("/html/body/input"));
clickbtn.click();
Thread.sleep(1500);
Alert alert = Demo11_alert_driver.switchTo().alert();
Thread.sleep(1500);
alert.accept();//关掉alert
Thread.sleep(3000);
Demo11_alert_driver.quit(); //退出WebDriver
2. Confirm (偶尔见)
WebElement clickbtn = Demo11_confirm_driver.findElement(By.xpath("/html/body/input"));
clickbtn.click();
Thread.sleep(1500);
Alert confirm = Demo11_confirm_driver.switchTo().alert();
//确定
Thread.sleep(1500);
confirm.accept();//关掉alert(确定)
Thread.sleep(1500);
confirm.accept();//关掉alert(确定)
//取消
Thread.sleep(1500);
confirm.dismiss();//关掉alert(取消)
Thread.sleep(1500);
confirm.accept();//关掉alert(确定)
Thread.sleep(3000);
Demo11_confirm_driver.quit(); //退出WebDriver
3. Prompt (现在很少见)
WebElement clickbtn = Demo11_prompt_driver.findElement(By.xpath("//html/body/input"));
clickbtn.click();
Thread.sleep(1500);
Alert prompt = Demo11_prompt_driver.switchTo().alert();
//prompt.dismiss(); //取消
Thread.sleep(1500);
prompt.sendKeys("Neo");
Thread.sleep(1500);
prompt.accept();
Thread.sleep(3000);
Demo11_prompt_driver.quit(); //退出WebDriver
radio button & checkbox
1. radio button
System.setProperty("webdriver.chrome.driver","D:/Program Files/Java/jdk1.8.0_141/bin/chromedriver.exe");
WebDriver Demo12_radio_driver=new ChromeDriver();
Demo12_radio_driver.manage().window().maximize();//浏览器最大化
Thread.sleep(3000);
Demo12_radio_driver.get("file:///C:/Users/Administrator/Desktop/autoTest/bootstrap-snippet-login-form/radiobutton.html");
List<WebElement> radios =
Demo12_radio_driver.findElements(By.name("fruit"));
Thread.sleep(1500);
radios.get(1).click();
Thread.sleep(3000);
Demo12_radio_driver.quit(); //退出WebDriver
2. checkbox
System.setProperty("webdriver.chrome.driver", "D:/Program Files/Java/jdk1.8.0_141/bin/chromedriver.exe");
WebDriver Demo12_checkbox_driver=new ChromeDriver();
Demo12_checkbox_driver.manage().window().maximize();//浏览器最大化
Thread.sleep(3000);
Demo12_checkbox_driver.get("file:///C:/Users/Administrator/Desktop/autoTest/bootstrap-snippet-login-form/checkbox2.html");
WebElement checkbox =
Demo12_checkbox_driver.findElement(By.name("cars"));
Thread.sleep(1500);
checkbox.click();
Thread.sleep(3000);
Demo12_checkbox_driver.quit(); //退出WebDriver