TA的每日心情 | 开心 2015-10-19 13:26 |
---|
签到天数: 2 天 连续签到: 2 天 [LV.1]测试小兵
|
2#
楼主 |
发表于 2014-7-5 10:53:34
|
只看该作者
本帖最后由 kavensyw 于 2014-7-5 11:18 编辑
自己搞定:
- //import org.openqa.selenium.JavascriptExecutor;
- JavascriptExecutor jse = (JavascriptExecutor)driver;
- //获取验证码的绝对坐标、宽度、高度
- Long xValue = (Long)jse.executeScript("oRect = document.getElementById(\"validateCode\").getBoundingClientRect(); " +
- "return oRect.left+window.screenLeft + document.documentElement.scrollLeft - document.documentElement.clientLeft;");
- Long yValue = (Long)jse.executeScript("oRect = document.getElementById(\"validateCode\").getBoundingClientRect(); " +
- "return oRect.top+ window.screenTop + document.documentElement.scrollTop - document.documentElement.clientTop;");
- Double width = (Double)jse.executeScript("oRect = document.getElementById(\"validateCode\").getBoundingClientRect(); return oRect.width;");
- Double height = (Double)jse.executeScript("oRect = document.getElementById(\"validateCode\").getBoundingClientRect(); return oRect.height;");
-
- Thread.sleep(3000);
- //使用Robot截图
- BtiCommon.captureScreen("Test111"
- ,Integer.parseInt(String.valueOf(xValue))
- ,Integer.parseInt(String.valueOf(yValue))
- ,Integer.parseInt(new java.text.DecimalFormat("0").format(width))
- ,Integer.parseInt(new java.text.DecimalFormat("0").format(height))
- );
-
复制代码
- import java.awt.image.BufferedImage;
- import java.awt.Robot;
- import java.awt.Rectangle;
- import java.io.FileOutputStream;
- import com.sun.image.codec.jpeg.*;
- public static void captureScreen(String screenName, int x, int y,
- int width, int height) {
- try {
- BufferedImage capture = null;
- Rectangle area = new Rectangle(x, y, width, height);
- Robot robot = new Robot();
- capture = robot.createScreenCapture(area);
- String fn = "E:\\" + screenName + ".jpg";
- FileOutputStream out = new FileOutputStream(fn);
- JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
- encoder.encode(capture);
- out.flush();
- out.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
复制代码 |
|