无法使用Selenium Webdriver 3.0.0-beta3捕获完整的网页屏幕截图

VISHVAMBRUTHJAVAGALTHIMMEGOWDA

大家好,

我编写了以下代码来捕获整个网页的屏幕快照。

但是我只能捕获显示的网页的部分/可见部分,而不捕获整个网页。请提出建议。

我正在使用:

Selenium WebDriver版本: 3.0.0-beta3

Firefox版本: 49.0.1

操作系统: Win10

package Selenium_WebDriver_Part1;
    import java.io.File;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.concurrent.TimeUnit;

    import org.apache.commons.io.FileUtils;
    import org.junit.Test;
    import org.openqa.selenium.By;
    import org.openqa.selenium.OutputType;
    import org.openqa.selenium.TakesScreenshot;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;

    public class Quikr {
        public static WebDriver driver=null;
        @Test
        public void loginTest() throws InterruptedException, IOException{
            System.setProperty("webdriver.gecko.driver","C:\\Eclipse\\Drivers\\geckodriver.exe");
            driver = new FirefoxDriver();
            driver.get("http://www.quikr.com/");
            driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
            takeScreenShot("LoginPage");
    }   
        public static void takeScreenShot(String fileName) throws IOException{
            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy hh mm ss");
            String time = dateFormat.format(now);
            String resDir = System.getProperty("user.dir")+"\\"+time;
            File resFolder = new File(resDir);
            resFolder.mkdir();
            File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            // Now you can do whatever you need to do with it, for example copy somewhere
            FileUtils.copyFile(scrFile, new File(resDir+"\\"+fileName+".jpeg"));
        }
    }
哈里斯·埃坎巴拉姆

我最好的建议是使用第三方图像处理库Ashot,它几乎可以解决大多数此类问题,并提供图像比较等。一旦导入AShot,请使用以下代码获取网页的整个屏幕截图。

public byte[] takeScreenshot(){
    byte[] img = null;
    Screenshot screenshot= new AShot()
            .shootingStrategy(ShootingStrategies.viewportPasting(100))
            .takeScreenshot(driver);
    BufferedImage image = screenshot.getImage();

    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write( image, "jpg", baos );
        baos.flush();
        byte[] imageInByte = baos.toByteArray();
        baos.close();
        img = imageInByte;
        InputStream in = new ByteArrayInputStream(imageInByte);
        BufferedImage bImageFromConvert = ImageIO.read(in);
        URL location = CodeLocations.codeLocationFromClass(this.getClass());
        String path = location.toString().substring(location.toString().indexOf("file:/")+5,location.toString().length());
        ImageIO.write(bImageFromConvert, "jpg", new File(
                path+"/screenshot.jpg"));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return img;
}

尝试AShot并分享您的想法

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法使用 0 宽度截取屏幕截图

我希望输出像[[0,0,0,0,],[0,1,0,0],[0,2,0,0],[0,3,0,0]]

Selenium :: WebDriver :: Error :: JavascriptError:arguments [0]未定义

Selenium Webdriver Java无法处理javascript:void(0),打开新标签

无法通过python3启动Selenium Safari WebDriver

reg [7:0] a [3:0] 和 reg [7:0] a [0:3] 有什么区别

使用Scala收集方法可帮助将[0,0,0,1,1,1,1,0,0,1,1]的列表转换为[3,4,2,2]

Java脚本:3 + 0 = 30

CakePHP 3:findById大于0

使用Selenium WebDriver的Web的屏幕截图部分

我无法运行C3P0ConnectionProvider

joomla 3错误:0无法打开文件以写入日志

使用d3.js将x轴移动到图表上的坐标(0,0)

在C#中使用Selenium ChromeDriver在W3C模式下(Selenium :: WebDriver :: Error :: UnknownCommandError)无法调用非W3C标准命令

Chrome Webdriver Selenium内存错误-Python 3

无法将“__NSCFNumber”(0x1105dc3c0)类型的值转换为“NSString”Swift 3

带有[0cf3:3004] Atheros适配器的Ubuntu 16.04无法使用蓝牙

尽管使用 c3p0,但“无法打开连接”问题

仅使用2或3从0达到N的方法有几种?

验证使用JPA运行的c3p0

使用C3P0的JDBC连接池

使用c3p0.idle_test_period。

参数匹配器的使用无效 0 匹配预期。3 记录

无法启动服务,并且线程未捕获而退出(group = 0xb2a3fba8)

zookeeper无法打开localhost / 0:0:0:0:0:0:0:0:1:2181的套接字

该代码的含义([1、2、3] || 0)[0]

Gekko if3 适用于 0 和 -0

通过Selenium WebDriver从JavascriptExecutor接口使用executeScript方法时,arguments [0]和arguments [1]是什么意思?

使用request.getRemoteAddr()返回0:0:0:0:0:0:0:0:1