selenium webdriver学习(一)
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()+"\n"+driver.getCurrentUrl()); driver.quit(); } }
package http;
import org.openqa.selenium.Alert;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class selenium {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
String URL="http://www.baidu.com";
System.setProperty("webdriver.ie.driver", "E:\\IEDriverServer.exe");
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
WebDriver driver = new InternetExplorerDriver(ieCapabilities) ;
driver.manage().window().maximize();
driver.get(URL);
/* Navigation navigation = driver.navigate();
navigation.to(URL); */
// Alert a= driver.switchTo().alert();
// a.accept();
Thread.sleep(2000);
// WebElement keyWord = driver.findElement(By.id("kw1"));
WebElement keyWord = driver.findElement(By.id("kw1"));
// WebElement keyWord = driver.findElement(By.xpath("//input[@id='kw']"));
WebElement f=driver.findElement(By.name("f"));
System.out.println(f.getText());
if(keyWord.isDisplayed())
{
keyWord.sendKeys("Selenium");
}
else
{
System.out.print("can't fund\n");
}
((JavascriptExecutor)driver).executeScript("alert(\"hello,this is a alert!\");value=\"Alert\"");
// Thread.sleep(3000);
Alert alert=driver.switchTo().alert();
System.out.println(alert.getText());
alert.dismiss();
WebElement submit = driver.findElement(By.id("su1"));
Thread.sleep(2000);
if(submit.isDisplayed())
{
submit.click();
}
else
{
driver.quit();
}
Thread.sleep(5000);
System.out.println(driver.getTitle());
// navigation.back();
Thread.sleep(5000);
// System.out.println(driver.getPageSource());
System.out.println(driver.getTitle()+"\n"+driver.getCurrentUrl());
driver.quit();
}
}
转载请注明出处:http://www.cnblogs.com/tobecrazy/

浙公网安备 33010602011771号