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 ...
随机推荐
- MVC中在RAZOR 模板里突然了现了 CANNOT RESOLVE SYMBOL ‘VIEWBAG’ 的错误提示
然后在Razor中出现了@ViewBag的不可用,@Url不可用,@Html 这些变量都不能用了. 异常提示: 编译器错误消息: CS0426: 类型“XX.Model.System”中不存在类型名称 ...
- js控制div滚动条,滚动滚动条使div中的元素可见并居中
1.html代码如下 <div id="panel"> <div id="div1"></div> <div id=& ...
- Ubuntu16.04 LTS下apt安装WireShark
Ubuntu16.04 LTS下apt安装WireShark 安装与配置 首先通过apt安装WireShark: $ sudo apt install wireshark 会同时安装许多的依赖包,其中 ...
- 资源描述结构(Resource Description Framework,RDF)
资源描述框架(Resource Description Framework),一种用于描述Web资源的标记语言.RDF是一个处理元数据的XML(标准通用标记语言的子集)应用,所谓元数据,就是" ...
- Babel:JavaScript编译器
一.介绍: Babel是一个Javascript编译器,可以将ES6语法转换成ES5. 这意味着,你可以现在就用ES6编写程序,而不用担心现有环境是否支持.下面是一个例子: //转码前: input. ...
- 延迟加载外部js文件,延迟加载图片(jquery.lazyload.js和echo,js)
js里一说到延迟加载,大都离不开两种情形,即外部Js文件的延迟加载,以及网页图片的延迟加载: 1.首先简单说一下js文件的3种延迟加载方式: (1)<script type="text ...
- NC 销售订单
主表:so_sale,主键:csaleid 子表1:so_saleorder_b 主键:corder_bid 子表2:so_saleexecute 主键:csale_bid 要求子表1和子表2 主键相 ...
- [转] JAVA读取excel数据(插入oracle数据库)
原文地址:http://blog.csdn.net/zczzsq/article/details/16803349 本实例做的是读取execl(只能读取.xls的execl,即只能读取03版的),如果 ...
- 跟着《beginning jquery》学写slider插件并借助自定义事件改进它
<beginning jquery>是一本很不错的学习jquery的书,作者的讲解深入浅出,很适合初学者,在最后一章里面,作者把前面所有的点结合起来完成了一个轮播图的jquery插件.实现 ...
- mysql Packet for query is too large (1185 > 1024)异常
注:最近mysql一直提示如下错误 Packet for query is too large (1185 > 1024). You can change this value on the s ...