Как сделать снимок экрана веб-элемента внутри веб-страницы, но не на весь экран или страницу через Selenium - PullRequest
0 голосов
/ 25 августа 2018

Я должен сделать снимок экрана с изображением определенного веб-сайта.Может быть, это 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"));

Screenshot, what I want to do with selenium

Ответы [ 2 ]

0 голосов
/ 28 августа 2018

Если ваш код пробует getScreenshotAs(), метод сделает снимок экрана всей страницы.

Чтобы сделать снимок экрана WebElement на определенной веб-странице, вы можете использовать метод AShot(), импортирующий ashot-1.4.4.jar во время работы с клиентом Selenium Java v3.14.0 , ChromeDriver v2.41 , Chrome v 68.0 .

Примечание : AShot() метод из ashot-1.4.4.jar работает только с jQuery включено веб-приложения .

Так как веб-сайт http://www.google.com/ не jQuery включен AShot() метод из ashot-1.4.4.jar не сможет сделать необходимый скриншот.

В качестве примера мы возьмем снимок с веб-сайта 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();
        }
    }
    
  • Скриншот:

elementScreenshot.png

0 голосов
/ 27 августа 2018

Можете ли вы попробовать это

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