1.百度搜索目標--實例html
boolean flag; //點擊次數 int shcs=3; int i=0; //窗口焦點控制 String detailWindowHandle = null; //火狐瀏覽器 System.setProperty("webdriver.firefox.bin", "D:/Softwareinstallation/huohu/firefox.exe"); WebDriver driver = new FirefoxDriver(); driver.get("http://www.baidu.com"); driver.manage().window().maximize(); //獲取主窗口header信息 String mainWindowHandle=driver.getWindowHandle(); Thread.sleep(2000); driver.findElement(By.id("kw")).sendKeys("17"); driver.findElement(By.id("su")).click(); Thread.sleep(3000); flag=bmm.isElementExsit(driver, By.linkText("17XUEe遊戲學院首頁")); System.out.println("flag="+flag); while(!flag) { System.out.println("ssssssss"); driver.findElement(By.linkText("下一頁>")).click(); Thread.sleep(2000); flag=bmm.isElementExsit(driver, By.linkText("17XUEe遊戲學院首頁")); Thread.sleep(500); } while(i<shcs){ //點擊遊戲學院 driver.findElement(By.linkText("17XUEe遊戲學院首頁")).click(); //獲取新窗口焦點 切換窗口 Set<String> handles=driver.getWindowHandles(); System.out.println(handles); Iterator<String> itWin=handles.iterator(); while(itWin.hasNext()){ String key=itWin.next(); if(mainWindowHandle.equals(key)){ continue; } detailWindowHandle=key; } WebDriver detailWindow = driver.switchTo().window(detailWindowHandle); Thread.sleep(2000); //關閉新打開的窗口 detailWindow.close(); handles.remove(detailWindowHandle); Thread.sleep(2000); //切換到主窗口 driver=driver.switchTo().window(mainWindowHandle); i++; System.out.println("點擊第"+i+"次"); } Thread.sleep(3000); driver.close(); driver.quit(); }
其中驗證元素是否存在代碼以下:java
/** * 驗證元素是否存在 * */ public boolean isElementExsit(WebDriver driver, By locator) { boolean flag = false; try { WebElement element=driver.findElement(locator); flag=null!=element; } catch (NoSuchElementException e) { System.out.println("Element:" + locator.toString() + " is not exsit!"); flag=false; } return flag; }
2.截圖函數node
/** * 頁面截圖,並保存到指定位置 * */ public void pagePrintscreen(WebDriver driver,String paths){ File screenshotfile= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); try { FileUtils.copyFile(screenshotfile, new File(paths)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
3.截屏並保存到指定文件夾web
/**
* 頁面 截圖
* driver
* 自定義圖片存放文件夾
* */
public void takeScreenshot(WebDriver driver,String customtestfname){
String screanName=String.valueOf(new Date().getTime())+".jpg";
File dir=new File("testresult/images/"+customtestfname);
//建立存放圖片的目錄
if(!dir.exists())
dir.mkdirs();
String screanPath=dir.getAbsolutePath()+"/"+screanName;
System.out.println("圖片路徑:"+screanPath);
//把圖片路徑寫入圖片流
try {
File scrFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile,new File(screanPath));
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("screen shot error:"+screanPath);
}
}瀏覽器
使用方法takeScreenshot(driver,"dl");函數
4.::::借鑑別人的::::post
頁面HTML是這樣的:ui
JavascriptExecutor j=(JavascriptExecutor)driver;url
j.executeScript("document.getElementById('li').style.display='block';");spa
driver.findElement(By.xpath("Xpth路徑")).click();