继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

selenium+java 笔记(5)

麦芒
关注TA
已关注
手记 48
粉丝 172
获赞 904

上传文件

1.定位input标签上传

//部分代码,创建书籍上传书籍封面
driver.findElement(By.cssSelector("input[type='file']")).sendKeys("D:\\tutu\\SoyuzReturn_ZH-CN9848773206_1920x1080.jpg");

2.AotuIt上传
它是一个使用类似BASIC脚本语言的免费软件,它被设计用来进行Windows GUI(图形用户界面)的自动化测试。

Runtime rn = Runtime.getRuntime();
try{
    String str = "执行文件路径";
    rn.exec(str);
}catch(Exception e){
    System.out.println("执行文件错误");
}

建议使用第一种方法。

操作Cookie

//获取所有的Cookie
Set<Cookie> cookies = driver.manage().getCookies();
//添加Cookie
Cookie c1 = new Cookie("name","jujuji");
driver.manage().addCookie(c1);
//获取指定字典对象
driver.manage().gerCookieNamed("name");
//删除指定字典对象
driver.manage().deleteCookieNamed("name");
//删除所有Cookie
driver.manage().deleteAllCookies();

调用JavaScript

//控制浏览器滚动条到指定位置
((JavascriptExecutor)driver).executeScript("window.scrollTo(1000,1000);");

窗口截图

1.截一屏的图

try{
    File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(srcFile,new File("F:\\test.png");
}catch(Exception e){
    e.printStackTrace();
}

2.截全图
PhantomJS下载地址
rasterize.js脚本地址

public Class Screenshot{
    public static viod mian(String[] args)throw IOException{
        //网址的url
        String htmlurl = "http://www.sina.com.cn/";
        //PhantomJS路径
        String phantomjs = "F:\\phantomjs-2.1.1-windows\\bin\\phantomjs";
        //rasterize.js路径
        String rasterize = "F:\\rasterize.js";
        Runtime rt = Runtime.getRuntime();
        rt.exec(phantomjs+" "+rasterize +" "+htmlurl+" "+"F:\\test.png");
    }
}
打开App,阅读手记
2人推荐
发表评论
随时随地看视频慕课网APP