1.验证码简单处理
1 /** 2 * 验证码等待输入函数 3 * */ 4 private void ZcYzm(WebDriver driver){ 5 boolean flag=false; 6 while(flag==false){ 7 //获取验证码文本框的值 8 WebElement zc_yzm=driver.findElement(By.name("ValidationText")); 9 String strzym=zc_yzm.getAttribute("value"); 10 //System.out.println("ss=="+strzym); 11 //注册码不能为空 12 if(strzym == null||"".equals(strzym)){ 13 //System.out.println("sssss"); 14 try { 15 Thread.sleep(1000); 16 } catch (InterruptedException e) { 17 // TODO Auto-generated catch block 18 e.printStackTrace(); 19 } 20 } 21 else 22 { 23 //验证码长度 24 int zym_len=strzym.length(); 25 if(zym_len >=6){ 26 System.out.println("您输入的验证码为:"+strzym); 27 flag=true; 28 } 29 } 30 31 } 32 }
2.验证页面元素是否存在
1 /** 2 * 验证元素是否存在 3 * */ 4 boolean isElementExsit(WebDriver driver, By locator) { 5 boolean flag = false; 6 try { 7 WebElement element=driver.findElement(locator); 8 flag=null!=element; 9 } catch (NoSuchElementException e) { 10 System.out.println("Element:" + locator.toString() 11 + " is not exsit!"); 12 } 13 return flag; 14 }
3.frame和iframe操作
1 //刷新右侧frame后,从新进入 2 driver.switchTo().frame("frmLinkPage2");
退出iframe操作
1 //退出iframe 2 driver.switchTo().defaultContent();
4.随机选择下列菜单内容
1 //所学专业1-4 2 int sxzy=rand.nextInt(3)+1; 3 System.out.println("所学专业::"+sxzy); 4 new Select(driver.findElement(By.id("profession"))).selectByIndex(sxzy);
5.随机输入内容
//随机数
Random rand=new Random();
1 //身份证号 2 String[] arraysf={"370802197703138738","140602198703258597","14103019760225449X","140426197508043799"}; 3 int sfzh=rand.nextInt(3); 4 System.out.println("性别::"+sfzh+"身份证:"+arraysf[sfzh]);
6.获取当前秒数
1 //获取当前秒数 2 Calendar calendar=Calendar.getInstance(); 3 int seconds=calendar.get(Calendar.SECOND);
7.获取窗口焦点
1 /** 2 * 获取新的窗口焦点,比如刷新什么的 3 * 新开窗口焦点获取函数 4 * */ 5 private int GainWindowFocus(WebDriver driver){ 6 int i=0; 7 for(String winHandle : driver.getWindowHandles()){ 8 System.out.println("+++" + winHandle); 9 driver.switchTo().window(winHandle); 10 i++; 11 } 12 return i; 13 }
8.读取文件写入记事本
1 /** 2 * 写入文件 3 * 记事本登录账号 4 * */ 5 private void Write_notepad(String name,String pass){ 6 try{ 7 String datas = name+" "+pass; 8 9 File file =new File("d:\17xuezh\dl.txt"); 10 11 //if file doesnt exists, then create it 12 //如果文件不存在,则创建它 13 if(!file.exists()){ 14 file.createNewFile(); 15 } 16 17 //true = append file true=追加文本内容 18 FileWriter fileWritter = new FileWriter(file.getPath(),true); 19 BufferedWriter bufferWritter = new BufferedWriter(fileWritter); 20 bufferWritter.write(datas); 21 bufferWritter.newLine(); 22 bufferWritter.close(); 23 System.out.println("Done ,完成!"); 24 25 } 26 catch(IOException e) 27 { 28 e.printStackTrace(); 29 } 30 }