//本来想着用execl来录入数据的,但是为了尽快完成所以直接搞了个数组
package aldtest; import org.openqa.selenium.*; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.Select; import sun.security.util.Length; public class mainbody { public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub String[] data = {"呵呵","哈哈","嘿嘿","吼吼"}; WebDriver driver = new ChromeDriver(); //打开网址 driver.get("http://***.com"); //设置浏览器大小,此次设置为最大化 driver.manage().window().maximize(); //选中账号框,输入账号密码登录 driver.findElement(By.id("userName")).clear(); driver.findElement(By.id("userName")).sendKeys("test"); driver.findElement(By.id("password")).clear(); driver.findElement(By.id("password")).sendKeys("123456"); driver.findElement(By.id("btnOk")).click(); Thread.sleep(4000); //进入首页后 driver.findElement(By.xpath("//*[@id='nav-menu']/li[6]/a")).click(); driver.findElement(By.xpath("//*[@id='nav-menu']/li[6]/ul/li[1]/a")).click(); //睡眠很重要,如果没有的话js文件未加载脚本找不到id执行不通过 Thread.sleep(2000); //循环添加数据 for(int a=0;a<data.length;a++) { Thread.sleep(6000); System.out.println(a); //添加数据点击加号 driver.findElement(By.id("addIcon")).click(); Thread.sleep(2000); driver.findElement(By.id("meterName")).sendKeys(data[a]); driver.findElement(By.id("brand")).sendKeys("ald"); //下拉框选择 driver.findElement(By.id("meterTypeli")).click(); driver.findElement(By.xpath("//*[@id='meterConfig']/div/div[2]/div/div[2]/div[1]/div[5]/div[2]/ul/li[1]")).click(); Thread.sleep(2000); //使用JavaScript输出 //((JavascriptExecutor)driver).executeScript("alert("hello,this is a alert!");value="Alert""); // driver.findElement(By.xpath("//*[@id='datatable']/tbody/tr[4]/td[1]/input")).click(); driver.findElement(By.xpath("//*[@id='datatable']/tbody/tr[4]/td[1]/input")).clear(); driver.findElement(By.xpath("//*[@id='datatable']/tbody/tr[4]/td[1]/input")).sendKeys(data[a]+"用"); //点击保存 driver.findElement(By.id("saveMeter")).click(); } Thread.sleep(4000); driver.quit(); //driver.close(); } }