zero_hu

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
 1 package code;
 2 
 3 import org.openqa.selenium.By;
 4 import org.openqa.selenium.WebDriver;
 5 import org.openqa.selenium.WebElement;
 6 import org.openqa.selenium.htmlunit.HtmlUnitDriver;
 7 
 8 
 9 public class UnitDriver {
10 
11     public static void main(String[] args) {
12 
13         //实例化虚拟浏览器对象
14         WebDriver driver = new HtmlUnitDriver();
15         //打开百度首页
16         String url = "http://www.baidu.com";
17         driver.get(url);    
18         //定位搜索框元素
19         WebElement ele = driver.findElement(By.id("kw"));
20         //输入需查询内容
21         ele.sendKeys("Cheese");
22         ele.submit();
23         
24         //获取页面标题
25         System.out.println("Page title is :" + driver.getTitle());
26         //获取页面url
27         System.out.println("Page url is :" + driver.getCurrentUrl());    
28         //关闭driver
29         driver.close();
30     }
31 
32 }

爬虫类场景可以采用这种方式

posted on 2018-05-25 16:26  zero_hu  阅读(1764)  评论(2编辑  收藏  举报