如何在selenium中获取新窗口的屏幕截图,并在截屏后,它应该导航回上一页

时间:2015-06-17 11:27:56

标签: selenium selenium-webdriver

我已经使用下面的代码截取了一个新窗口的截图,但它正在拍摄上一个窗口的截图。

driver.findElement(By.xpath("html/body/div[2]/div[1]/div[1]/div[1]/a")).click();  
Thread.sleep(15000);
File scrFile18 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile18, new File("d:/Users/ Updates.png"));

3 个答案:

答案 0 :(得分:2)

WebDriver driver;
driver=new FirefoxDriver();
driver.manage().window().maximize();//maximize
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);//wait
driver.get("http://www.dittotv.com/");//url
driver.findElement(By.xpath("//img[@alt='Windows']")).click();
String parentHandle = driver.getWindowHandle();

for (String winHandle : driver.getWindowHandles()) {
    driver.switchTo().window(winHandle); 
}
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("E:\\screenshot.jpg"));``
driver.close();
driver.switchTo().window(parentHandle);

答案 1 :(得分:0)

您需要在截屏前切换到新窗口。请查看此stackoverflow answer

答案 2 :(得分:0)

检查

var webdriver = require('selenium-webdriver');
    var fs = require('fs');

    var driver = new webdriver.Builder().build();

    webdriver.WebDriver.prototype.saveScreenshot = function(filename) {
        return driver.takeScreenshot().then(function(data) {
            fs.writeFile(filename, data.replace(/^data:image\/png;base64,/,''), 'base64', function(err) {
                if(err) throw err;
            });
        })
    };


    // example usage
    driver.saveScreenshot('mypage.png');