zoukankan      html  css  js  c++  java
  • 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()+"
    "+driver.getCurrentUrl());
             
         
             
              driver.quit();
               
           
        }
        
    }
    View Code
    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
    ");
             }
             
             ((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()+"
    "+driver.getCurrentUrl()); 
              driver.quit();
    	}
    
    }
    
  • 相关阅读:
    面试热点|理解TCP/IP传输层拥塞控制算法
    mysql中使用存储过程方法中的注意事项
    mysql 游标的使用方法
    php mkdir No such file or director问题
    curl 出现错误的调试方法
    xp与win7双系统时删除win7启动菜单
    退回win7后无法上网 的解决方法
    Windows7安装程序无法定位现有系统分区,也无法创建新的系统分区
    YII 框架在windows系统下的安装
    php 在服务器端开启错误日志记录方法
  • 原文地址:https://www.cnblogs.com/tobecrazy/p/3585608.html
Copyright © 2011-2022 走看看