Selenium WebDriver Code
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的更多相关文章
- selenium webdriver 右键另存为下载文件(结合robot and autoIt)
首先感谢Lakshay Sharma 大神的指导 最近一直在研究selenium webdriver右键菜单,发现selenium webdriver 无法操作浏览器右键菜单,如图 如果我想右键另存为 ...
- Selenium WebDriver使用IE浏览器
摘:http://www.cnblogs.com/dream0577/archive/2012/10/07/2714579.html /** 用IE驱动,1.先到官网下载IEDriverS ...
- Selenium WebDriver + Python 环境配置
1. 下载必要工具及安装包 1.1.[Python开发环境] 下载并安装Python 2.7.x版本(当前支持2.x版本,不要下载最新的3.X的版本因为python3并非完全兼容python2) ...
- Selenium Tutorial (1) - Starting with Selenium WebDriver
Starting with Selenium WebDriver Selenium WebDriver - Introduction & Features How Selenium WebDr ...
- 【转】Watir, Selenium & WebDriver
转自:http://watirmelon.com/2010/04/10/watir-selenium-webdriver/ Please also see my new ‘Watir-WebDrive ...
- Selenium WebDriver + Grid2 + RSpec之旅(二)----Grid2的配置
Selenium WebDriver + Grid2 + RSpec之旅(二) ----Grid2的配置 为什么要使用Selenium-Grid 分布式运行大规模的TestCase 能够通过一个中央节 ...
- Selenium webdriver 操作IE浏览器
V1.0版本:直接新建WebDriver使用 import org.openqa.selenium.WebDriver; import org.openqa.selenium.ie.InternetE ...
- Selenium webdriver 开始
最早接触的selenium是 selenium IDE,当时是为了准备论文.为了用IDE还下载了Firefox浏览器.后来接触过两个项目都需要selenium,一个采用selenium webdirv ...
- Selenium WebDriver java 简单实例
开发环境 JDK 下载地址: http://www.oracle.com/technetwork/java/javase/downloads/index.html Eclipse: 下载地址:http ...
随机推荐
- 《Note --- Unreal 4 --- matinee》
https://docs.unrealengine.com/latest/CHN/Engine/Matinee/index.html https://docs.unrealengine.com/lat ...
- PAT 1044. 火星数字(20)
火星人是以13进制计数的: 地球人的0被火星人称为tret. 地球人数字1到12的火星文分别为:jan, feb, mar, apr, may, jun, jly, aug, sep, oct, no ...
- U3D 打包时找不到tag的问题
在公司改完一个功能,把工程拷回家打开后,编辑器模式下运行正常,打包PC平台和安卓平台时都报错,找不到chatContent这个tag,察看了下下拉列表中明明有这个tag,并且勾选上了,但是点击add ...
- [LeetCode] Minimum Depth of Binary Tree 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- Java反射机制
Java反射机制 一:什么事反射机制 简单地说,就是程序运行时能够通过反射的到类的所有信息,只需要获得类名,方法名,属性名. 二:为什么要用反射: 静态编译:在编译时确定类型,绑定对象,即通过 ...
- NDT(Normal Distribution Transform) 算法(与ICP对比)和一些常见配准算法
原文地址:http://ghx0x0.github.io/2014/12/30/NDT-match/ By GH 发表于 12月 30 2014 目前三维配准中用的较多的是ICP迭代算法,需要提供一个 ...
- 使用EXtjs6.2构建web项目
一.项目简介 众所周知ext是一款非常强大的表格控件,尤其是里边的grid为用户提供了非常多的功能,现在主流的还是用extjs4.0-4.2,但是更高一点的版本更加符合人的审美要求.因此,在今天咱们构 ...
- [HTML5] FileReader对象
写在前面 前一篇文章介绍了HTML5中的Blob对象(详情戳这里),从中了解到Blob对象只是二进制数据的容器,本身并不能操作二进制,故本篇将对其操作对象FileReader进行介绍. FileRea ...
- <form:select>的使用
最近在学习springMVC,用到了<form:select>标签,使用发过程中遇到了些问题,现在记录下,以防忘记. 我jsp页面是这样的: <%@ page language=&q ...
- vue-router
官方文档: 旧版:https://github.com/vuejs/vue-router/tree/1.0/docs/zh-cn 新版:http://router.vuejs.org/(2.0版本) ...