分享

selenium webdriver元素是否显示

 小猪窝969 2015-10-16
复制代码

 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();
           
       
    }
    
}

复制代码

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多