package baidu;
import java.io.File;
import java.io.IOException;
import junit.framework.TestCase;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
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 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);
/*
Navigation navigation = driver.navigate();
navigation.to(URL);*/
Thread.sleep(2000);
//WebElement reg=driver.findElement(By.name("tj_reg"));
//reg.click();
// WebElement keyWord = driver.findElement(By.id("kw1"));
WebElement keyWord = driver.findElement(By.xpath("//input[@id='kw1']"));
keyWord.clear();
keyWord.sendKeys("Selenium");
Thread.sleep(3000);
WebElement submit = driver.findElement(By.id("su1"));
System.out.println(submit.getLocation());
submit.click();
System.out.println(driver.getWindowHandle());
Thread.sleep(5000);
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
try {
FileUtils.copyFile(scrFile, new File("E:\screenshot.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println(driver.getPageSource());
String pageSource=driver.getPageSource();
System.out.println(pageSource);
WebElement webElement =driver.findElement(By.xpath("/html"));
if(pageSource.matches("http://www.baidu.com/link?"))
{
System.out.println("*************PASS***********");
}
else
{
System.out.println("*************FAIL***********");
}
System.out.println(webElement.getText());
System.out.println(driver.getTitle());
Thread.sleep(5000);
// navigation.back();
System.out.println(driver.getTitle()+"
"+driver.getCurrentUrl());
driver.quit();
}
}