selenium webdriver学习(一)
package baidu; import java.io.File;
import java.io.IOException; import junit.framework.TestCase; import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver; public class selenium { public static void main (String [] args) throws InterruptedException
{ String URL="http://www.baidu.com";
System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get(URL); /*
Navigation navigation = driver.navigate();
navigation.to(URL);*/
Thread.sleep(2000); //WebElement reg=driver.findElement(By.name("tj_reg"));
//reg.click();
// WebElement keyWord = driver.findElement(By.id("kw1"));
WebElement keyWord = driver.findElement(By.xpath("//input[@id='kw1']")); keyWord.clear();
keyWord.sendKeys("Selenium");
Thread.sleep(3000); WebElement submit = driver.findElement(By.id("su1")); System.out.println(submit.getLocation());
submit.click();
System.out.println(driver.getWindowHandle());
Thread.sleep(5000);
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
try {
FileUtils.copyFile(scrFile, new File("E:\\screenshot.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println(driver.getPageSource());
String pageSource=driver.getPageSource();
System.out.println(pageSource);
WebElement webElement =driver.findElement(By.xpath("/html"));
if(pageSource.matches("http://www.baidu.com/link?"))
{
System.out.println("*************PASS***********");
}
else
{
System.out.println("*************FAIL***********");
}
System.out.println(webElement.getText());
System.out.println(driver.getTitle());
Thread.sleep(5000);
// navigation.back(); System.out.println(driver.getTitle()+"\n"+driver.getCurrentUrl()); driver.quit(); } }
package http; import org.openqa.selenium.Alert; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities; public class selenium { /**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub String URL="http://www.baidu.com";
System.setProperty("webdriver.ie.driver", "E:\\IEDriverServer.exe");
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
WebDriver driver = new InternetExplorerDriver(ieCapabilities) ;
driver.manage().window().maximize(); driver.get(URL); /* Navigation navigation = driver.navigate();
navigation.to(URL); */ // Alert a= driver.switchTo().alert();
// a.accept();
Thread.sleep(2000);
// WebElement keyWord = driver.findElement(By.id("kw1")); WebElement keyWord = driver.findElement(By.id("kw1")); // WebElement keyWord = driver.findElement(By.xpath("//input[@id='kw']")); WebElement f=driver.findElement(By.name("f")); System.out.println(f.getText());
if(keyWord.isDisplayed())
{
keyWord.sendKeys("Selenium");
}
else {
System.out.print("can't fund\n");
} ((JavascriptExecutor)driver).executeScript("alert(\"hello,this is a alert!\");value=\"Alert\""); // Thread.sleep(3000); Alert alert=driver.switchTo().alert();
System.out.println(alert.getText()); alert.dismiss(); WebElement submit = driver.findElement(By.id("su1"));
Thread.sleep(2000);
if(submit.isDisplayed())
{
submit.click();
}
else
{
driver.quit();
}
Thread.sleep(5000);
System.out.println(driver.getTitle()); // navigation.back(); Thread.sleep(5000);
// System.out.println(driver.getPageSource());
System.out.println(driver.getTitle()+"\n"+driver.getCurrentUrl());
driver.quit();
} }
selenium webdriver学习(一)的更多相关文章
- Selenium webdriver 学习总结-元素定位
Selenium webdriver 学习总结-元素定位 webdriver提供了丰富的API,有多种定位策略:id,name,css选择器,xpath等,其中css选择器定位元素效率相比xpath要 ...
- (java)selenium webdriver学习---实现简单的翻页,将页面内容的标题和标题链接取出
selenium webdriver学习---实现简单的翻页,将页面内容的标题和标题链接取出: 该情况适合能能循环page=1~n,并且每个网页随着循环可以打开的情况, 注意一定是自己拼接的url可以 ...
- (java)selenium webdriver学习---三种等待时间方法:显式等待,隐式等待,强制等待
selenium webdriver学习---三种等待时间方法:显式等待,隐式等待,强制等待 本例包括窗口最大化,刷新,切换到指定窗口,后退,前进,获取当前窗口url等操作: import java. ...
- (java)selenium webdriver学习--打开新窗口,并判断新窗口是否与目标窗口一致
描述:selenium webdriver学习--打开新窗口,并判断新窗口是否与目标窗口一致,若一致则切换到该窗口并获取标题 跳出if判断,获取父级标题,并关闭 HTML标签不太明显时,可以用路径表示 ...
- (java)selenium webdriver学习,选择模块,点击下一页,获取当前url
selenium webdriver学习,选择模块,点击下一页,获取当前url 查找下一页有多种方法,这里列举两种: isSelected()函数用于判断是否点击选中,返回Boolean类型 impo ...
- (java)selenium webdriver学习--通过id、name定位,输入内容,搜索,关闭操作、通过tagname查找元素
selenium webdriver学习--通过id.name定位,输入内容,搜索,关闭操作:通过tagname查找元素 打开谷歌浏览器,输入不同的网站,搜索框的定位含有不同元素(有时为id,有时为n ...
- selenium webdriver学习(六)------------如何得到弹出窗口
selenium webdriver学习(六)------------如何得到弹出窗口 在selenium 1.X里面得到弹出窗口是一件比较麻烦的事,特别是新开窗口没有id.name的时候.当时还整理 ...
- selenium webdriver学习(十)------------如何把一个元素拖放到另一个元素里面(转)
selenium webdriver学习(十)------------如何把一个元素拖放到另一个元素里面 博客分类: Selenium-webdriver 元素拖放drag and drop Q群里 ...
- selenium webdriver学习(九)------------如何操作cookies(转)
selenium webdriver学习(九)------------如何操作cookies 博客分类: Selenium-webdriver Web 测试中我们经常会接触到Cookies,一个C ...
- selenium webdriver学习(八)------------如何操作select下拉框(转)
selenium webdriver学习(八)------------如何操作select下拉框 博客分类: Selenium-webdriver 下面我们来看一下selenium webdriv ...
随机推荐
- linuxMint install shuruma
http://my.oschina.net/u/1446273/blog/306053 http://blog.sina.com.cn/s/blog_5d406a1b0101jlgn.html htt ...
- 关于JS的几点TIPS
作为前端基本工作每天都会用到JS...但是我们对JS真的都了解吗,或者说有什么tips是我们不知道的呢.. So..此文关于JS的几点tips..... 一:定时器(可传多个参数) 首先是一个一般的定 ...
- 时间处理工具类DateUtils
public class DateUtils { public static final String SHORT_DATE ...
- 知识梳理HTML篇
HTML 浏览器内核: IE:trident Firefox : gecko Safari/chrome : webkit Opera : presto(新 ...
- webapp中的meta
<!--开发后删除--> <meta http-equiv="Pragma" name="no-store" /><!--必须联网 ...
- [设计模式] javascript 之 策略模式
策略模式说明 定义: 封装一系列的算法,使得他们之间可以相互替换,本模式使用算法独立于使用它的客户的变化. 说明:策略模式,是一种组织算法的模式,核心不在于算法,而在于组织一系列的算法,并且如何去使用 ...
- ACM_1 大数求和
/*1 *2014.11.18 *大数求和 */ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <strin ...
- HDOJ 3507 Print Article
Print Article Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)To ...
- SqlServer中字符串和日期类型的转换
SQL Server Date 函数 定义和用法 CONVERT() 函数是把日期转换为新数据类型的通用函数. CONVERT() 函数可以用不同的格式显示日期/时间数据. 语法 CONVERT(da ...
- Mac Pro 入门、遇到的问题、个性化设置 汇总
入门资料 入门一:Mac 基本用法 入门二:Mac 使用VMware Fusion虚拟机 入门三:Mac 使用brew安装软件 问题汇总 [问题1]如何复制文本? 一只手指头按下,另外一只手指头滑动选 ...