java-selenium入门demo(无需启动浏览器)


 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 }

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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM