首页 > 其他 > 详细

使用Chrome Driver 的示例

时间:2015-05-29 17:11:36      阅读:243      评论:0      收藏:0      [点我收藏+]

//导入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();

  }

}

使用Chrome Driver 的示例

原文:http://www.cnblogs.com/feifeidxl/p/4538629.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!