[Selenium] 使用Chrome Driver 的示例

//导入Selenium 库和 ChromeDriver 库

pachage com.learningselenium.simplewebdriver;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class testChromeDriver{

  static Thread thread = new Thread();  //开启一个延时的线程,用于处理页面出错的情况

  public static void main(String[] args){

    //通过以下方式来加载ChromeDriver, 其中第二个参数为chromedriver 的具体路径

    System.setProperty("webdriver.chrome.driver", "/Selenium 2/selenium/chromedriver");

    //通过ChromeDriver 打开Google Chrome 浏览器并访问百度主页

    WebDriver driver = new ChromeDriver();

    driver.get("http://baidu.com");

    //显示的让ChromeDriver 等待10秒让百度主页完全加载成功

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    //如果搜索输入框为可编辑状态

    if (driver.findElement(By.id("kw").isEnabled()){

      System.out.println("Baidu search text box is editable!");

      driver.findElement(By.id("kw")).sendKeys("selenium");

      driver.findElement(By.id("su")).click();

    }else{

      System.out.println("Baidu Search text box is not editable!");

    }

    try{

      thread.sleep(5000);

    }catch (Exception e) {

      System.out.println("Error");

    }

    driver.close();

  }

}