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. IISExpress Log 文件路径

    问题 用VS做开发时经常用IISExpress测试web程序,那么在测试过程中生成的Log文件放在哪里了? 答案 情况1 默认情况下 applicationhost.config 文中定义了连个日志文 ...

  2. 字符串匹配算法--Brute-Force算法

    Brute-Force(暴力)算法是字符串匹配中最简单也是最容易理解的算法. 主要思想是 按顺序遍历母串,将每个字符作为匹配的起始字符,判断是否匹配字串.若第一个字符与字串匹配,则比较下一个字符,否则 ...

  3. 高级查询---嵌套and分页

    高级嵌套语句: 子查询: 语句: select * from 表名 where 列名= ( 子查询语句 ) 注意:子查询语句必须放在小括号呢 可以使用< >=等运算符号,sql serve ...

  4. 2015最流行的Android组件、工具、框架大全

    Android 是目前最流行的移动操作系统之一. 随着新版本的不断发布, Android的功能也日益强大, 涌现了很多流行的应用程序, 也催生了一大批的优秀的组件. 本文试图将目前流行的组件收集起来以 ...

  5. 图片下方出现多3px的原因及解决方法

    产生原因:主要是因为图片的垂直对齐方式vertical-align引发的,默认值是baseline,默认为此值时图片下方就会多出3px. 解决方案: 1.将图片的垂直对齐方式vertical-alig ...

  6. oracle函数

    oracle中函数分为3大类,分别为:单行函数,分组函数,分析函数. 1.单行函数 单行函数同时只能对一行进行操作,且对输入的每一行返回一行输出结果. 单行函数还分为:字符函数,数子函数,日期函数,转 ...

  7. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  8. SQLite剖析之存储模型

    前言 SQLite作为嵌入式数据库,通常针对的应用的数据量相对于DBMS的数据量小.所以它的存储模型设计得非常简单,总的来说,SQLite把一个数据文件分成若干大小相等的页面,然后以B树的形式来组织这 ...

  9. flexbox布局神器

    前言 2009年,W3C提出了一种新的方案----Flexbox布局(弹性布局),可以简便.完整.响应式地实现各种页面布局.Flex布局模型不同于块和内联模型布局,块和内联模型的布局计算依赖于块和内联 ...

  10. EventBus使用

    转自 http://blog.csdn.net/harvic880925/article/details/40660137