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 ...
随机推荐
- ORACLE 利用 REPLACE函数替换字段字符串
REPLACE(string,s1,s2) string 希望被替换的字符或变量 s1 被替换的字符串 s2 要替换的字符串 SQL> select replace(he love you,he ...
- Appium+python的一个简单完整的用例
最近一直在忙,终于有时间来整理一下,传一个简单的用例,运行之后可以看到用例的报告,希望对大家有帮助. HTMLTestRunner这个包网上有很多,大家可以自己下载. 1 import unittes ...
- ngx_http_upstream_module模块.md
ngx_http_upstream_module ngx_http_upstream_module模块用于定义可由proxy_pass,fastcgi_pass,uwsgi_pass,scgi_pas ...
- JAVA设计模式之2-简单工厂模式
今天来介绍简单工厂模式,工厂模式包括简单工厂模式和抽象工厂模式,今天先讲简单工厂模式,然后引申到抽象工厂模式 在没有工厂模式前,我们创建类是直接new一个对象,比如下面所示,北汽有两个小型SUV,分别 ...
- [LeetCode] Queue Reconstruction by Height 根据高度重建队列
Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...
- [LeetCode] Reverse String 翻转字符串
Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...
- [LeetCode] Rotate Array 旋转数组
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- [LeetCode] Word Search 词语搜索
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- poj2217
Secretary Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1257 Accepted: 515 Descript ...
- 关于delphi exit 继承
父类窗体frm 继承下来一个子类 form2 父类按钮代码 procedure Tfrm.Button1Click(Sender: TObject); begin ShowMessage('); Ex ...