您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

使用selenium 截屏生成图片的方法

2024/4/25 9:29:25发布3次查看
当需要保存网页上的图片到本地时,使用到网页截图。
        由于selenium jar包提供的driver,模拟页面操作,却没有右击属性。可以通过模拟鼠标右击的形式,进行图片保存至本地的操作。步骤繁琐不说还需要引入别的jar包,个人认为没必要大费周章,使用selenium jar包takesscreenshot属性进行截屏一样可以达到生成图片目的。
操作步骤:
                   1 获取图片元素,得到图片位置和大小 webelement imgelement = driver.findelement(by.id("img")); point location = webelement.getlocation(); // 获得位置。 dimension size = webelement.getsize(); // 大小
2 创建全屏截图,通过imageio 读取形式 takesscreenshot takesscreenshot = (takesscreenshot) driver; bufferedimage originalimage = imageio.read(new bytearrayinputstream(takesscreenshot.getscreenshotas(outputtype.bytes)));
3 截取图片,生成bufferedimage bufferedimage croppedimage = originalimage.getsubimage( location.getx(), location.gety(), size.getwidth(), size.getheight());
4 本地生成图片 string fileurl = ""; // 图片路径
file file = new file(fileurl); imageio.write(croppedimage , "png", file); // 内容写入
以上就是使用selenium 截屏生成图片的方法 的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录