1.百度搜索目标--实例
boolean flag;
//点击次数
int shcs=3;
int i=0;
//窗口焦点控制
String detailWindowHandle = null;
//火狐浏览器
System.setProperty("webdriver.firefox.bin", "D:/Softwareinstallation/huohu/firefox.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com");
driver.manage().window().maximize();
//获取主窗口header信息
String mainWindowHandle=driver.getWindowHandle();
Thread.sleep(2000);
driver.findElement(By.id("kw")).sendKeys("17");
driver.findElement(By.id("su")).click();
Thread.sleep(3000);
flag=bmm.isElementExsit(driver, By.linkText("17XUEe游戏学院首页"));
System.out.println("flag="+flag);
while(!flag)
{
System.out.println("ssssssss");
driver.findElement(By.linkText("下一页>")).click();
Thread.sleep(2000);
flag=bmm.isElementExsit(driver, By.linkText("17XUEe游戏学院首页"));
Thread.sleep(500); }
while(i<shcs){
//点击游戏学院
driver.findElement(By.linkText("17XUEe游戏学院首页")).click();
//获取新窗口焦点 切换窗口
Set<String> handles=driver.getWindowHandles();
System.out.println(handles);
Iterator<String> itWin=handles.iterator();
while(itWin.hasNext()){
String key=itWin.next();
if(mainWindowHandle.equals(key)){
continue;
}
detailWindowHandle=key;
}
WebDriver detailWindow = driver.switchTo().window(detailWindowHandle);
Thread.sleep(2000);
//关闭新打开的窗口
detailWindow.close();
handles.remove(detailWindowHandle);
Thread.sleep(2000);
//切换到主窗口
driver=driver.switchTo().window(mainWindowHandle);
i++;
System.out.println("点击第"+i+"次");
}
Thread.sleep(3000);
driver.close();
driver.quit();
}
其中验证元素是否存在代码如下:
/**
* 验证元素是否存在
* */
public boolean isElementExsit(WebDriver driver, By locator) {
boolean flag = false;
try {
WebElement element=driver.findElement(locator);
flag=null!=element;
} catch (NoSuchElementException e) {
System.out.println("Element:" + locator.toString()
+ " is not exsit!");
flag=false; }
return flag;
}
2.截图函数
/**
* 页面截图,并保存到指定位置
* */
public void pagePrintscreen(WebDriver driver,String paths){
File screenshotfile= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(screenshotfile, new File(paths));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
3.截屏并保存到指定文件夹
/**
* 页面 截图
* driver
* 自定义图片存放文件夹
* */
public void takeScreenshot(WebDriver driver,String customtestfname){
String screanName=String.valueOf(new Date().getTime())+".jpg";
File dir=new File("testresult/images/"+customtestfname);
//创建存放图片的目录
if(!dir.exists())
dir.mkdirs();
String screanPath=dir.getAbsolutePath()+"/"+screanName;
System.out.println("图片路径:"+screanPath);
//把图片路径写入图片流
try {
File scrFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile,new File(screanPath));
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("screen shot error:"+screanPath);
}
}
使用方法takeScreenshot(driver,"dl");
4.::::借鉴别人的::::
selenium 怎么处理display:none
页面HTML是这样的:
<div class="cf w index-middle">
<div class="mask" style="display: none; opacity: 0.3;">
JavascriptExecutor j=(JavascriptExecutor)driver;
j.executeScript("document.getElementById('li').style.display='block';");
driver.findElement(By.xpath("Xpth路径")).click();