在验证某些关键步骤时,需要截个图来记录一下当时的情况
Webdriver截图时,需要引入
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
截图方法
public static void snapshot(TakesScreenshot drivername, String filename)
{
// this method will take screen shot ,require two parameters ,one is driver name, another is file name String currentPath = System.getProperty("user.dir"); //get current work folder
System.out.println(currentPath);
File scrFile = drivername.getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
try {
System.out.println("save snapshot path is:"+currentPath+"/"+filename);
FileUtils.copyFile(scrFile, new File(currentPath+"\\"+filename));
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Can't save screenshot");
e.printStackTrace();
}
finally
{ System.out.println("screen shot finished");
}
}
以下任务:
1.使用selenium打开百度,截图;
2.输入selenium关键字,截图;
3.搜索 并打开 selenium的百度百科,截图;
具体代码如下:
package baidu; import java.io.File;
import java.io.IOException; import org.apache.commons.io.FileUtils; import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver; public class selenium { public static void snapshot(TakesScreenshot drivername, String filename)
{
// this method will take screen shot ,require two parameters ,one is driver name, another is file name File scrFile = drivername.getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
try {
System.out.println("save snapshot path is:E:/"+filename);
FileUtils.copyFile(scrFile, new File("E:\\"+filename));
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Can't save screenshot");
e.printStackTrace();
}
finally
{
System.out.println("screen shot finished");
}
} public static void main (String [] args) throws InterruptedException
{ String URL="http://www.baidu.com";
System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get(URL);
//max size the browser
driver.manage().window().maximize();
/*
Navigation navigation = driver.navigate();
navigation.to(URL);*/
Thread.sleep(2000);
snapshot((TakesScreenshot)driver,"open_baidu.png");
//WebElement reg=driver.findElement(By.name("tj_reg"));
//reg.click();
// WebElement keyWord = driver.findElement(By.id("kw1")); //find the element
WebElement keyWord = driver.findElement(By.xpath("//input[@id='kw1']"));
keyWord.clear();
//send key words
keyWord.sendKeys("Selenium");
Thread.sleep(3000);
snapshot((TakesScreenshot)driver,"input_keyWord.png"); WebElement submit = driver.findElement(By.id("su1")); System.out.println(submit.getLocation());
submit.click();
//System.out.println(driver.getWindowHandle());
Thread.sleep(5000); // System.out.println(driver.getPageSource()); String pageSource=driver.getPageSource();
// System.out.println(pageSource);
//WebElement link =driver.findElement(By.xpath(SELENIUM_LINK));
WebElement link =driver.findElement(By.xpath("//*[@id=\"1\"]/h3/a")); //*[@id="1"]/h3/a
link.click();
Thread.sleep(5000);
driver.switchTo().window(driver.getWindowHandles().toArray(new String[0])[1]); //get page title
System.out.println(driver.getTitle());
Thread.sleep(5000);
// navigation.back();
snapshot((TakesScreenshot)driver,"open_bake.png");
System.out.println(driver.getTitle()+"\n"+driver.getCurrentUrl()); driver.quit(); } }
在百度搜索结果中,拿到你想要的elements,可以使用浏览器的查看元素,通过xpath方法获取
运行此代码后截图效果如下:
console输出:
Starting ChromeDriver (v2.9.248315) on port 33834
save snapshot path is:E:/open_baidu.png
screen shot finished
save snapshot path is:E:/input_keyWord.png
screen shot finished
(858, 179)
Selenium_百度百科
save snapshot path is:E:/open_bake.png
screen shot finished
Selenium_百度百科
http://baike.baidu.com/subview/478050/6464537.htm?fr=aladdin