How can I take a screenshot with Selenium WebDriver?

后端 未结 30 2632
不知归路
不知归路 2020-11-21 07:48

Is it possible to take a screenshot using Selenium WebDriver?

(Note: Not Selenium Remote Control)

30条回答
  •  误落风尘
    2020-11-21 08:17

    Java

    I could not get the accepted answer to work, but as per the current WebDriver documentation, the following worked fine for me with Java 7 on OS X v10.9 (Mavericks):

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

提交回复
热议问题