是否可以使用Selenium WebDriver进行截图?

(注:不含硒遥控器)


当前回答

JAVA

嗨,你可以在selenium中截图,这里是下面给出的代码,你可以在任何selenium项目中创建截图,我们也可以在aws中为失败的场景生成截图。

    public boolean takeScreenshot(final String name) {
    String screenshotDirectory = System.getenv("WORKING_DIRECTORY");
    File screenshot = 
    ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    return screenshot.renameTo(new File(screenshotDirectory, 
    String.format("%s.png", name)));
    }

其他回答

是的,可以使用Selenium WebDriver对网页进行快照。

WebDriver API提供的getScreenshotAs()方法为我们做了这项工作。

语法:getScreenshotAs(OutputType<X> target)

返回类型:X

参数:target—查看OutputType提供的选项

适用性:不特定于任何DOM元素

例子:

    TakesScreenshot screenshot = (TakesScreenshot) driver;
    File file = screenshot.getScreenshotAs(OutputType.FILE);

有关详细信息,请参阅下面的工作代码片段。

    public class TakeScreenShotDemo {

        public static void main(String[] args) {
            WebDriver driver = new FirefoxDriver();
            driver.manage().window().maximize();
            driver.get(“http: //www.google.com”);

            TakesScreenshot screenshot = (TakesScreenshot) driver;

            File file = screenshot.getScreenshotAs(OutputType.FILE);

            // Creating a destination file
            File destination = new File(“newFilePath(e.g.: C: \\Folder\\ Desktop\\ snapshot.png)”);
            try {
                FileUtils.copyFile(file, destination);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

访问Snapshot using WebDriver获取更多详细信息。

Java

我无法得到可接受的答案,但根据当前的WebDriver文档,以下内容对我来说在OS X v10.9 (Mavericks)上的Java 7工作得很好:

import java.io.File;
import java.net.URL;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class Testing {

   public void myTest() throws Exception {
       WebDriver driver = new RemoteWebDriver(
               new URL("http://localhost:4444/wd/hub"),
               DesiredCapabilities.firefox());

       driver.get("http://www.google.com");

       // RemoteWebDriver does not implement the TakesScreenshot class
       // if the driver does have the Capabilities to take a screenshot
       // then Augmenter will add the TakesScreenshot methods to the instance
       WebDriver augmentedDriver = new Augmenter().augment(driver);
       File screenshot = ((TakesScreenshot)augmentedDriver).
               getScreenshotAs(OutputType.FILE);
   }
}

通过Selenium的Java和Python客户端,有多种方法可以使用Selenium WebDriver进行截图。


Java方法

下面是不同的Java截图方法:

Using getScreenshotAs() from the TakesScreenshot interface: Code block: package screenShot; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; 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 Firefox_takesScreenshot { public static void main(String[] args) throws IOException { System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://login.bws.birst.com/login.html/"); new WebDriverWait(driver, 20).until(ExpectedConditions.titleContains("Birst")); File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(scrFile, new File(".\\Screenshots\\Mads_Cruz_screenshot.png")); driver.quit(); } } Screenshot: If the webpage is jQuery enabled, you can use ashot from the pazone/ashot library: Code block: package screenShot; import java.io.File; import javax.imageio.ImageIO; 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; import ru.yandex.qatools.ashot.AShot; import ru.yandex.qatools.ashot.Screenshot; import ru.yandex.qatools.ashot.shooting.ShootingStrategies; public class ashot_CompletePage_Firefox { public static void main(String[] args) throws Exception { System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://jquery.com/"); new WebDriverWait(driver, 20).until(ExpectedConditions.titleContains("jQuery")); Screenshot myScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(100)).takeScreenshot(driver); ImageIO.write(myScreenshot.getImage(),"PNG",new File("./Screenshots/firefoxScreenshot.png")); driver.quit(); } } Screenshot: Using selenium-shutterbug from assertthat/selenium-shutterbug library: Code block: package screenShot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import com.assertthat.selenium_shutterbug.core.Shutterbug; import com.assertthat.selenium_shutterbug.utils.web.ScrollStrategy; public class selenium_shutterbug_fullpage_firefox { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.google.co.in"); Shutterbug.shootPage(driver, ScrollStrategy.BOTH_DIRECTIONS).save("./Screenshots/"); driver.quit(); } } Screenshot:


Python方法

以下是Python中截图的不同方法:

Using save_screenshot() method: Code block: from selenium import webdriver driver = webdriver.Chrome(r'C:\Utility\BrowserDrivers\chromedriver.exe') driver.get("http://google.com") driver.save_screenshot('./Screenshots/save_screenshot_method.png') driver.quit() Screenshot: Using the get_screenshot_as_file() method: Code block: from selenium import webdriver driver = webdriver.Chrome(r'C:\Utility\BrowserDrivers\chromedriver.exe') driver.get("http://google.com") driver.get_screenshot_as_file('./Screenshots/get_screenshot_as_file_method.png') driver.quit() Screenshot: Using get_screenshot_as_png() method: Code block: from selenium import webdriver driver = webdriver.Chrome(r'C:\Utility\BrowserDrivers\chromedriver.exe') driver.get("http://google.com") screenPnG = driver.get_screenshot_as_png() # Crop it back to the window size (it may be taller) box = (0, 0, 1366, 728) im = Image.open(BytesIO(screenPnG)) region = im.crop(box) region.save('./Screenshots/get_screenshot_as_png_method.png', 'PNG', optimize=True, quality=95) driver.quit() Screenshot:

Jython

import org.openqa.selenium.OutputType as OutputType
import org.apache.commons.io.FileUtils as FileUtils
import java.io.File as File
import org.openqa.selenium.firefox.FirefoxDriver as FirefoxDriver

self.driver = FirefoxDriver()
tempfile = self.driver.getScreenshotAs(OutputType.FILE)
FileUtils.copyFile(tempfile, File("C:\\screenshot.png"))

Python

webdriver.get_screenshot_as_file(filepath)

上述方法将截取屏幕截图,并将其作为文件存储在作为参数提供的位置中。