【问题标题】:Unable to capture full webpage screenshot using selenium webdriver 3.0.0-beta3无法使用 selenium webdriver 3.0.0-beta3 捕获完整的网页截图
【发布时间】:2017-02-10 19:56:27
【问题描述】:

大家好,

我编写了以下代码来捕获整个屏幕截图 网页。

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

我正在使用:

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"));
        }
    }

【问题讨论】:

    标签: selenium selenium-webdriver webdriver


    【解决方案1】:

    我正在使用 selenium 3.0.0 beta4 Firefox v.43.0.1 OS Windows7 而不是 System.setProperty("webdriver.gecko.driver","C:\Eclipse\Drivers\geckodriver.exe");使用以下行 System.setProperty("webdriver.firefox.marionette","D:\Testing\WebDriver\geckodriver.exe");

    【讨论】:

      【解决方案2】:

      我最好的建议是使用第三方图像处理库 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 并分享你的想法

      【讨论】:

        【解决方案3】:

        这就是它应该的工作方式。如果你想捕获整个网页,那么你必须将整个内容缩小到可见区域,然后截取屏幕截图。

        【讨论】:

          【解决方案4】:

          为了捕获整个页面的屏幕截图,您需要在 ((TakesScreenshot)driver).getScreenshotAs... 语句之前添加一行代码。

          driver = new Augmenter().augment(driver);
          

          【讨论】:

            猜你喜欢
            • 2020-01-29
            • 2017-02-03
            • 2023-01-23
            • 2012-10-26
            • 1970-01-01
            • 2023-03-09
            • 2018-11-25
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多