无法使用Marionette / Firefox驱动程序截取屏幕截图

时间:2018-01-11 23:57:35

标签: java firefox selenium-webdriver

使用Marionette geckodriver版本0.19.1。

以下是设置Marionette / Firefox功能的方法:

select AVG(o.zMeanPSFMagStd) into mydb.z1819 
from MeanObject as o 
join ObjectThin ot on ot.ObjID=o.ObjID 
inner join StackObjectAttributes soa on soa.objID=o.objid 
where o.zMeanPSFMag>=18 
    and o.zMeanPSFMag<19 
    and o.zQfPerfect > 0.85 
    and (ot.qualityFlag & 1) = 0 
    and (ot.qualityFlag & 2) = 0 
    and o.zMeanPSFMagErr <> -999 
    and o.zMeanPSFMagStd <> -999 
    and ot.nz > 10 and soa.zpsfMajorFWHM < 6  
    and soa.zpsfMinorFWHM/nullif(soa.zpsfMajorFWHM,0) > 0.65 
    and soa.zpsfFlux/nullif(soa.zpsfFluxErr,0)>5 
    and (ot.b>10 or ot.b<-10) 
    and (ot.raMean>0 and ot.raMean<180) 

这是我尝试截屏的地方。此代码在Chrome中运行良好,最初在Firefox中运行,但现在由于无法运行而失败。

private static DesiredCapabilities setMarionetteCapabilities() {
    setMarionetteDriver(); // sets the correct path to the executable

    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("browser.startup.homepage", "about:blank");

    FirefoxOptions options = new FirefoxOptions();

    DesiredCapabilities capabilities = DesiredCapabilities.firefox();
    capabilities.setCapability("marionette", true);
    capabilities.setCapability(FirefoxDriver.PROFILE, profile);
    capabilities.setCapability(FirefoxOptions.FIREFOX_OPTIONS, options);
    return capabilities;
}

但每当我调用该方法时,它只会回复它不能拍摄屏幕截图。以下是getCapabilities()的结果:

public static String takeScreenShot(String caption)  {
    System.out.println( "Test: " + ((HasCapabilities)driver).getCapabilities());

    if(!((HasCapabilities)driver).getCapabilities().is(CapabilityType.TAKES_SCREENSHOT)) {
        System.out.println("Cannot take a screenshot");
        return "";
    }

    TakesScreenshot camera = (TakesScreenshot)driver;
    File scrFile = camera.getScreenshotAs(OutputType.FILE);
    String filename = getFilename(caption);

    try {
        FileUtils.moveFile(scrFile, new File(captureDir + sep + filename));
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("Saved to: " + captureDir + sep + filename);
    return filename;
}

显然,没有列出任何截屏功能。

我已经尝试了

[  
   {  
      moz:profile=C:\Users\____\AppData\Local\Temp\rust_mozprofile.9tfK2yUnv4jV,
      rotatable=false,
      timeouts=      {  
         implicit=0,
         pageLoad=300000,
         script=30000
      },
      pageLoadStrategy=normal,
      moz:headless=false,
      platform=XP,
      moz:accessibilityChecks=false,
      acceptInsecureCerts=true,
      browserVersion=57.0.4,
      platformVersion=10.0,
      moz:processID=11332,
      browserName=firefox,
      javascriptEnabled=true,
      platformName=XP,
      moz:webdriverClick=false
   }
]

但这似乎没有做任何事情。

那么如何添加捕获屏幕截图的功能呢?

注意:我目前在Windows 10计算机上运行它,但它也将在Mac和Linux系统上运行,因此答案需要与操作系统无关。

1 个答案:

答案 0 :(得分:1)

好的,愚蠢的回答

木偶可以截取屏幕截图;它只是没有报告它可以。一旦我在方法开始时删除了测试,一切都运行良好。以下是感兴趣的人的最终方法:

public static String takeScreenShot(String caption)  {
    String filename = getFilename(caption);

    try {
        TakesScreenshot camera = (TakesScreenshot)driver;
        File scrFile = camera.getScreenshotAs(OutputType.FILE);

        FileUtils.moveFile(scrFile, new File(captureDir + sep + filename));
        System.out.println("Saved to: " + captureDir + sep + filename);
    } catch (Exception e) {
        System.out.println("Cannot take a screenshot");
    }

    return filename;
}