Selenium WebDriver 用于模拟浏览器的功能,可以做网站测试用,也可以用来做crawler。我是用eclipse开发的,导入selenium-server-standalone-***.jar(Right click project -->Properties --> Java Buid Path --> Libraries --> Add External Jar...)。这个包可以在Selenium官网下载。

下面的代码是简单的跟一个网站做交互:

 public class IndeedJobSearch {

     public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub //Create firefox driver to drive the browser
//File pathBinary = new File("D:\\Programes\\tools\\FireFox\\firefox.exe");
//FirefoxBinary Binary = new FirefoxBinary(pathBinary);
//FirefoxProfile firefoxPro = new FirefoxProfile();
//WebDriver driver = new FirefoxDriver(Binary, firefoxPro);
//ChromeDriver or ie
System.setProperty("webdriver.chrome.driver",//webdriver.ie.driver
"D:\\Projects\\JavaWorkspace\\ThirdPartyLibs\\WebDriver\\chromedriver.exe");//IEDriverServer.exe
WebDriver driver = new ChromeDriver(); //InternetExplorerDriver //Open Indeed home page
driver.get("http://www.indeed.hk/");
//Find what field and enter Selenium
Thread.sleep(2000);
driver.findElement(By.id("what")).sendKeys("Selenium"); //"findElements" will return all
//Find location field and enter London
driver.findElement(By.id("where")).clear();
Thread.sleep(2000);
driver.findElement(By.id("where")).sendKeys("Hong Kong");
//Find FindJobs button and click on it
Thread.sleep(2000);
driver.findElement(By.id("fj")).click();
//From job search results page, get page title and jobs count message System.out.println(driver.getTitle());
System.out.println(driver.findElement(By.id("searchCount")).getText()); driver.close();
} }

IndeedJobSearch.java

要抓数据就要用方法来定位数据,下面是普遍用到的数据定位方法

 public class LocatingStrategies {

     public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub //Create firefox driver to drive the browser
File pathBinary = new File("D:\\Programes\\tools\\FireFox\\firefox.exe");
FirefoxBinary Binary = new FirefoxBinary(pathBinary);
FirefoxProfile firefoxPro = new FirefoxProfile();
WebDriver driver = new FirefoxDriver(Binary, firefoxPro); //Open Indeed home page
driver.get("http://www.indeed.hk/"); //Locating by ID
//driver.findElement(By.id("what")).sendKeys("Selenium"); //"findElements" will return all //Locating by name
//driver.findElement(By.name("q")).sendKeys("Selenium"); //Locating by LinkText
//driver.findElement(By.linkText("建立個人檔案")).click(); //Locating by partialLinkText
//driver.findElement(By.partialLinkText("招聘廣告")).click(); //Locating by Xpath By.xpath("//input[@placeholder='email']")).sendKeys("User NAme");
// System.out.println(
//
// driver.findElement(By.xpath("//img[@title='Indeed 香港']"))
// .getAttribute("src")
//
// ); //Locating by CssSelector By.cssSelector("css=input.input_submit")).click();
//driver.findElement(By.cssSelector("input.input_submit")).click(); //Locating by Tagname
System.out.println( driver.findElements(By.tagName("a")).size() ); //Locating by ClassName
System.out.println( driver.findElements(By.className("input_text")).size() );
driver.findElement(By.className("input_text")).sendKeys("Selenium");
} }

LocatingStrategies.java

对javacript的popups: alert, prompt, confirm box的交互处理:

 public class JavaScriptPopups {

     public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
"D:\\Projects\\JavaWorkspace\\ThirdPartyLibs\\WebDriver\\chromedriver.exe");//IEDriverServer.exe
WebDriver driver = new ChromeDriver(); driver.get("http://test1.absofttrainings.com/javascript-alert-confirm-prompt-boxes/");
System.out.println( driver.findElements(By.className("headerText")).size() );
//Thread.sleep(6000);
driver.findElement(By.xpath("//button[text()='Display Alert Box']")).click(); Alert alert = driver.switchTo().alert();
System.out.println(
"Alert text: " + alert.getText()
);
Thread.sleep(2000);
alert.accept();
driver.switchTo().defaultContent();
System.out.println(driver.findElement(By.id("confirm")).getText()
);
driver.findElement(By.id("prompt")).click();
alert = driver.switchTo().alert();
Thread.sleep(3000);
alert.sendKeys("Bruce");
Thread.sleep(3000);
alert.accept();
driver.switchTo().defaultContent(); }
}

JavaScriptPopups.java

Selenium WebDriver Code的更多相关文章

  1. selenium webdriver 右键另存为下载文件(结合robot and autoIt)

    首先感谢Lakshay Sharma 大神的指导 最近一直在研究selenium webdriver右键菜单,发现selenium webdriver 无法操作浏览器右键菜单,如图 如果我想右键另存为 ...

  2. Selenium WebDriver使用IE浏览器

    摘:http://www.cnblogs.com/dream0577/archive/2012/10/07/2714579.html /**       用IE驱动,1.先到官网下载IEDriverS ...

  3. Selenium WebDriver + Python 环境配置

    1.   下载必要工具及安装包 1.1.[Python开发环境] 下载并安装Python 2.7.x版本(当前支持2.x版本,不要下载最新的3.X的版本因为python3并非完全兼容python2) ...

  4. Selenium Tutorial (1) - Starting with Selenium WebDriver

    Starting with Selenium WebDriver Selenium WebDriver - Introduction & Features How Selenium WebDr ...

  5. 【转】Watir, Selenium & WebDriver

    转自:http://watirmelon.com/2010/04/10/watir-selenium-webdriver/ Please also see my new ‘Watir-WebDrive ...

  6. Selenium WebDriver + Grid2 + RSpec之旅(二)----Grid2的配置

    Selenium WebDriver + Grid2 + RSpec之旅(二) ----Grid2的配置 为什么要使用Selenium-Grid 分布式运行大规模的TestCase 能够通过一个中央节 ...

  7. Selenium webdriver 操作IE浏览器

    V1.0版本:直接新建WebDriver使用 import org.openqa.selenium.WebDriver; import org.openqa.selenium.ie.InternetE ...

  8. Selenium webdriver 开始

    最早接触的selenium是 selenium IDE,当时是为了准备论文.为了用IDE还下载了Firefox浏览器.后来接触过两个项目都需要selenium,一个采用selenium webdirv ...

  9. Selenium WebDriver java 简单实例

    开发环境 JDK 下载地址: http://www.oracle.com/technetwork/java/javase/downloads/index.html Eclipse: 下载地址:http ...

随机推荐

  1. 如何利用报表工具FineReport实现报表列的动态展示

    相信动态列的实现困扰了很多人,大数据量,多字段的加载将会非常耗时,数据又做不到真正的动态灵活.现有的方式都是通过变向的隐藏等方式来实现. 那该如何解决呢?这里分享帆软报表设计器FineReport的实 ...

  2. kubernetes部署Fluentd+Elasticsearch+kibana 日志收集系统

    一.介绍 1. Fluentd 是一个开源收集事件和日志系统,用与各node节点日志数据的收集.处理等等.详细介绍移步-->官方地址:http://fluentd.org/ 2. Elastic ...

  3. mysql workbench如何把已有的数据库导出ER模型

    mysql workbench的特长是创建表结构的,然后在结构图中,圈圈点点,很容易就利用可视化方式把数据库建好,然后再导入到数据库服务器中,这种办法很效率.但是有时我们有一个需求,事先没有建表结构模 ...

  4. const 引起的BUG

    今天白天出现了碰见了一个问题,隐藏得比较深,这里记录一下. 初衷很简单,就是要更改改一个数据库的链接名,这个链接名是放在数据层public const string connDB="conn ...

  5. IO流

    流的概念和作用 学习JavaIO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特 ...

  6. SQL基础语法(二)

    SQL SELECT 语句 本章讲解 SELECT 和 SELECT * 语句. SQL SELECT 语句 SELECT 语句用于从表中选取数据. 结果被存储在一个结果表中(称为结果集). SQL ...

  7. ubuntu下面更改用户名的方法

    在装HADOOP 系统时候不小心, 没有将三台机器的用户名设置为一致的用户名,导致后面发生很多麻烦.下面总结一下UBUNTU中改用户名的方法. 1.  先给系统添加一个super用户,我们用这个用户名 ...

  8. PHP 的 foreach

    foreach 可以 针对 string 操作,不过会生成一个警告,并跳过该 expression, 举例: $ids = '123'; foreach ($ids as $item){ print_ ...

  9. Mysql两个引擎对比

    Mysql两个引擎对比 MyIsam      优点:      1.支持B-Tree检索和文本全文检索      2.性能消耗方面相对较低      3.支持全表(table)锁      缺点: ...

  10. Android测试基础题(三)

    今天接着给大家带来的是Android测试基础题(三).    需求:定义一个排序的方法,根据用户传入的double类型数组进行排序,并返回排序后的数组 俗话说的好:温故而知新,可以为师矣 packag ...