selenium入门练习01代码补充

public class HelloSelenium {
public static void main(String[] args) throws InterruptedException, ParseException{
//window 环境
//System.setProperty("webdriver.firefox.bin", "C:\\Program Files (x86)\\Mozilla Firefox\\forefox.exe");
// System.setProperty("webdriver.ie.driver", "C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe");
// System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome");

//iMac 环境
// System.setProperty("webdriver.firefox.bin", "/Applications/Firefox.app/Contents/MacOS/firefox");
// System.setProperty("webdriver.chrome.driver", "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome");
// System.setProperty("webdriver.safari.bin", "/Applications/Safari.app/Contents/Safari");

WebDriver driver=new FirefoxDriver();
Navigation navigation=driver.navigate();
test8(navigation,driver);

System.out.println("close...");
Thread.sleep(2000);
driver.close();
}
/**
* @throws InterruptedException 
* method: to 、forward、refresh
*/
public static void test1(Navigation navigation,WebDriver driver) throws InterruptedException{
navigation.to("http://www.baidu.com");
navigation.to("http://www.360.cn");

Thread.sleep(2000);
navigation.back();
Thread.sleep(2000);
navigation.forward();
System.out.println("Refresh。。。");
Thread.sleep(3000);
navigation.refresh();

}
/**
* method: sendKeys、clear、submit、id、name、linkText:链接文本查找、partialLinkText:模糊查找、(className、tagName:对象隐藏时不怎么好用)、xpath、
*/
public static void test2(Navigation navigation,WebDriver driver){
navigation.to("http://www.baidu.com");
// WebElement baiduBox=driver.findElement(By.id("kw"));
// WebElement baiduBox=driver.findElement(By.name("wd"));
// baiduBox.sendKeys("selenium");

// WebElement baiduBox=driver.findElement(By.linkText("登录"));
// WebElement baiduBox=driver.findElement(By.partialLinkText("登"));
// WebElement baiduBox=driver.findElement(By.className("mnav"));//新闻
// WebElement baiduBox=driver.findElement(By.tagName("a"));
WebElement baiduBox=driver.findElement(By.xpath(".//*[@id='kw']"));
baiduBox.sendKeys("selenium");
}
/**
* 遍历 select 选项内容
*/
public static void test3(Navigation navigation,WebDriver driver){
navigation.to("http://www.baidu.com");
navigation.to("http://tieba.baidu.com/");
driver.findElement(By.xpath(".//*[@id='tb_header_search_form']/a")).click();
WebElement select= driver.findElement(By.name("sm"));
String targetText="按时间顺序"; //按时间顺序/按相关性排序
List<WebElement> options=select.findElements(By.tagName("option")); //获取列表集合
for(int i=0;i<options.size();i++){
if(options.get(i).getText().equals(targetText)){
options.get(i).click();
}
}
}
/**
* @param navigation
* @param driver
* method:getCurrentUrl、getTitle,isSelected
*/
public static void test4(Navigation navigation,WebDriver driver){
navigation.to("http://www.baidu.com");
// String title=driver.getTitle();
// System.out.println(title);


// String url=driver.getCurrentUrl();
// System.out.println(url);

// WebElement  link=driver.findElement(By.xpath(".//*[@id='u1']/a[7]"));
// String linkText=link.getText();
// System.out.println(linkText);
//.//*[@id='u1']/a[6] 点击登录
driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[3]/a[6]")).click();
WebElement checkbox=driver.findElement(By.id("TANGRAM__PSP_8__memberPass"));
boolean isSelected=checkbox.isSelected();
System.out.println(isSelected);
}
/**
*
* @param navigation
* @param driver
* @throws InterruptedException

* method:getWindowHandles,switchTo
*/
public static void test5(Navigation navigation,WebDriver driver) throws InterruptedException{
navigation.to("http://www.baidu.com");

driver.findElement(By.xpath(".//*[@id='u1']/a[6]")).click();

///html/body/div[5]/div[2]/div[2]/div/div/div/div/div/div[1]/form/p[7]/a[1].//*[@id='u1']/a[7]

driver.findElement(By.xpath("/html/body/div[5]/div[2]/div[2]/div/div/div/div/div/div[1]/form/p[7]/a[1]")).click();

Thread.sleep(2000);

//一组窗口句柄的数量

String[] handles=new String[driver.getWindowHandles().size()];

//返回一个数组包含在这个集合中的所有元素 

driver.getWindowHandles().toArray(handles);
for(int i=0;i<handles.length;i++){
System.out.println(handles[i]);
}
WebDriver childWindow=driver.switchTo().window(handles[1]); //更换窗口
WebElement textChild=childWindow.findElement(By.id("TANGRAM__PSP_4__account"));
textChild.sendKeys("964321735—金手指");
childWindow.close();
driver.switchTo().window(handles[0]);//切换回浏览器
}
/**
* @param navigation
* @param driver
* @throws InterruptedException, ParseException
* Cookie 操作:
*/
public static void test6(Navigation navigation,WebDriver driver) throws InterruptedException, ParseException{
navigation.to("http://www.baidu.com");
Set<Cookie>  cookies=driver.manage().getCookies();
Cookie[] allCookies=new Cookie[cookies.size()];
cookies.toArray(allCookies);

System.out.println("当前 cookie的数量"+cookies.size());
for(int i=0;i<cookies.size();i++){
System.out.println("cookie 的名称"+allCookies[i].getName());
System.out.println("cookie 的值"+allCookies[i].getValue());
System.out.println("cookie 所在域"+allCookies[i].getDomain());
System.out.println("cookie 路径"+allCookies[i].getPath());
System.out.println("cookie 过期时间"+allCookies[i].getExpiry());
System.out.println("----------The last is  Iterative Cookie-------------");
}

Calendar calendar=Calendar.getInstance();
Date date=calendar.getTime();
//添加cookie
Cookie newCookie=new Cookie("newCookie", " 新Cookie value","jsz_1000.cn","",new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ).parse("2015-2-12 19:20:00"));
cookies.add(newCookie);
System.out.println("---------new Cookie---------");
System.out.println("newCookie 的名称"+newCookie.getName());
System.out.println("newCookie 的值"+newCookie.getValue());
System.out.println("newCookie 所在域"+newCookie.getDomain());
System.out.println("newCookie 路径"+newCookie.getPath());
System.out.println("newCookie 过期时间"+newCookie.getExpiry());
System.out.println("----------next is  add  new Cookie-------------");

allCookies=new Cookie[cookies.size()];
cookies.toArray(allCookies);
//添加后cookies 的数量:
System.out.println("newCookies 的数量:"+cookies.size());
for(int i=0;i<cookies.size();i++){
System.out.println("cookie 的名称"+allCookies[i].getName());
System.out.println("cookie 的值"+allCookies[i].getValue());
System.out.println("cookie 所在域"+allCookies[i].getDomain());
System.out.println("cookie 路径"+allCookies[i].getPath());
System.out.println("cookie 过期时间"+allCookies[i].getExpiry());
System.out.println("=============================");
}
//删除cookie,
cookies.remove(allCookies[cookies.size()-1]);
//删除后的 cookie 数量显示:
System.out.println("删除后的数量:"+cookies.size());
//剩余的Cookie
System.out.println("----------剩余的Cookie--------");
allCookies=new Cookie[cookies.size()];
cookies.toArray(allCookies);
for(int i=0;i<cookies.size();i++){
System.out.println("cookie 的名称"+allCookies[i].getName());
System.out.println("cookie 的值"+allCookies[i].getValue());
System.out.println("cookie 所在域"+allCookies[i].getDomain());
System.out.println("cookie 路径"+allCookies[i].getPath());
System.out.println("cookie 过期时间"+allCookies[i].getExpiry());
System.out.println("==================================");
}
}
/**
* @param navigation
* @param driver
* @throws InterruptedException

* window
*/
public static void test7(Navigation navigation,WebDriver driver) throws InterruptedException{
navigation.to("http://www.baidu.com");
Window window=driver.manage().window();
//输出其坐标大小
System.out.println("最大化前, window 的屏幕上的坐标为:"+window.getPosition().x+","+window.getPosition().y);
System.out.println("最大化前, window 的屏幕上的高、宽为:"+window.getSize().height+","+window.getSize().width);

window.maximize();
Thread.sleep(1000);
//最大化后:
System.out.println("最大化后, window 的屏幕上的坐标为:"+window.getPosition().x+","+window.getPosition().y);
System.out.println("最大化后, window 的屏幕上的高、宽为:"+window.getSize().height+","+window.getSize().width);
}
public static void test8(Navigation navigation,WebDriver driver){

EventFiringWebDriver eventDriver=new EventFiringWebDriver(driver);
eventDriver.register(new MyWebDriverListener()); //引入自定义监听事件

Timeouts timeouts=eventDriver.manage().timeouts();

//异步执行超时时间设置

// timeouts.setScriptTimeout(10,TimeUnit.SECONDS);

//查找元素时,最大的等待时间设置

timeouts.implicitlyWait(10,TimeUnit.SECONDS);

navigation.to("http://www.baidu.com");
eventDriver.findElement(By.id("kw")).sendKeys("selenium");
eventDriver.findElement(By.id("su")).click();

eventDriver.findElement(By.id("xxxx"));
}

}

/**

 * @author 金手指

 *自定义监听事件

 */

public class MyWebDriverListenerimplements WebDriverEventListener {


@Override

public void beforeNavigateTo(String url, WebDriverdriver) {

System.out.println("页面跳转前的URL为:"+driver.getCurrentUrl());

}

@Override

public void afterNavigateTo(String url, WebDriverdriver) {

System.out.println("页面跳转后的URL为:"+driver.getCurrentUrl());

}

@Override

public void beforeNavigateBack(WebDriver driver) {

System.out.println("浏览器后退前,需要执行的代码");

}

@Override

public void afterNavigateBack(WebDriver driver) {

System.out.println("浏览器后退后,需要执行的代码");

}

@Override

public void beforeNavigateForward(WebDriver driver) {

System.out.println("浏览器前进前,需要执行的代码");

}

@Override

public void afterNavigateForward(WebDriver driver) {

System.out.println("浏览器前进后,需要执行的代码");

}

@Override

public void beforeFindBy(By by, WebElement element, WebDriver driver) {

System.out.println("浏览器查找元素前,需要执行的代码");

}

@Override

public void afterFindBy(By by, WebElement element, WebDriver driver) {

System.out.println("浏览器查找元素后,需要执行的代码");

}

@Override

public void beforeClickOn(WebElement element, WebDriverdriver) {

System.out.println("浏览器点击元素前值:"+element.getAttribute("value"));

}

@Override

public void afterClickOn(WebElement element, WebDriverdriver) {

System.out.println("浏览器点击元素后,"+element.getAttribute("value"));

}

@Override

public void beforeChangeValueOf(WebElement element, WebDriverdriver) {

System.out.println("浏览器变更元素值前值:"+element.getAttribute("value"));

}

@Override

public void afterChangeValueOf(WebElement element, WebDriverdriver) {

System.out.println("浏览器变更元素后,需要执行的代码");

}

@Override

public void beforeScript(String script, WebDriverdriver) {

System.out.println("浏览器脚本执行前,需要执行的代码");

}

@Override

public void afterScript(String script, WebDriverdriver) {

System.out.println("浏览器脚本执行后,需要执行的代码");

}

@Override

public void onException(Throwable throwable, WebDriverdriver) {

System.out.println("浏览器发生异常时,执行的代码----------");

Date date=new Date();

SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String namePic=formatter.format(date);//picture name

String currentPath = System.getProperty("user.dir");//get current work folder

File srcFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

// Now you can do whatever you need to do with it, for example copy somewhere

        try {

            FileUtils.copyFile(srcFile, new File(currentPath+"/"+namePic+".png"));

        } catch (Exception e) {

            System.out.println("Can't save screenshot");

            e.printStackTrace();

        }finally{

            System.out.println("screen shot finished");

            System.out.println("save snapshot path is:"+currentPath+"/"+namePic+".png");

            System.out.println("发生的异常,原因:"+throwable.getMessage());

            driver.close();//关闭浏览器, 为了停止虚拟机运行

        }

}


}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

csdn_金手指

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值