1

我必须捕获特定网站图像的屏幕截图。也许这是整个屏幕 20% 的折扣,我使用了下面的代码,它正在捕获整个屏幕。这无助于我解决问题。

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));

截图,我想用 selenium 做什么

4

3 回答 3

1

你能试试这个

driver.get("https://stackoverflow.com/");
WebElement element = driver.findElement(By.xpath("//span[(text()='Stack Overflow') and @class='-img _glyph']"));
WrapsDriver wrapsDriver = (WrapsDriver) element;
File screenshot = ((TakesScreenshot) wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.FILE);
Rectangle rectangle = new Rectangle(element.getSize().width, element.getSize().height, element.getSize().height, element.getSize().width);
Point location = element.getLocation();
BufferedImage bufferedImage = ImageIO.read(screenshot);
BufferedImage destImage = bufferedImage.getSubimage(location.x, location.y, rectangle.width, rectangle.height);
ImageIO.write(destImage, "png", screenshot);
File file = new File("C:\\123.png");
FileUtils.copyFile(screenshot, file);
于 2018-08-27T07:00:57.207 回答
1

根据您的代码试验getScreenshotAs()方法,将截取整个页面的屏幕截图。

要在特定网页中捕获WebElement的屏幕截图,您可以在使用Selenium Java Client v3.14.0ChromeDriver v2.41Chrome v 68.0AShot()时使用导入ashot-1.4.4.jar的方法。

注意ashot-1.4.4.jar 中的AShot()方法仅适用于启用jQuery的Web 应用程序

因此,由于网站http://www.google.com/不是ashot-1.4.4.jar中启用jQueryAShot()的方法,因此无法获取所需的屏幕截图。

作为一个例子,我们将从网站上 https://jquery.com/截取一张快照。

  • 代码块:

    package aShot;
    
    import java.io.File;
    
    import javax.imageio.ImageIO;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    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;
    
    public class ashot_google_homepage_logo {
    
        public static void main(String[] args) throws Exception {
    
            System.setProperty("god.bless.you", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("start-maximized");
            options.addArguments("disable-infobars");
            options.addArguments("--disable-extensions"); 
            WebDriver driver =  new ChromeDriver(options);
            driver.get("https://jquery.com/");
            WebElement myWebElement = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//h3[contains(.,'Lightweight Footprint')]")));
            Screenshot myScreenshot = new AShot().takeScreenshot(driver, myWebElement);
            ImageIO.write(myScreenshot.getImage(),"PNG",new File("./Screenshots/elementScreenshot.png"));
            driver.quit();
        }
    }
    
  • 截屏:

元素截图.png


参考

您可以在如何使用 Selenium WebDriver 截屏中找到详细讨论

于 2018-08-28T09:11:48.890 回答
0

我正在使用 selenium-java-3.141.59 和 ChromeDriver 83.0.4103.39,下面的代码非常适合我:

    WebDriver driver = new ChromeDriver();
   driver.get("https://www.google.com/");
  WebElement element = driver.findElement(By.id("hplogo"));
  Screenshot screenshotHeader = new AShot().coordsProvider(new WebDriverCoordsProvider()).shootingStrategy(ShootingStrategies.viewportPasting(100)).takeScreenshot(driver, element);
    try {
        ImageIO.write(screenshotHeader.getImage(),"jpg",new File("C:/TESTSELENIUM/Google.jpg"));
    } catch (IOException e) {
        e.printStackTrace();
    }
于 2020-06-30T05:33:01.043 回答