pazone / ashot

WebDriver Screenshot utility. Take screenshots, crop, prettify, compare
Other
636 stars 157 forks source link

Not able to get web element screenshot correctly. #147

Open eugenechang2002 opened 5 years ago

eugenechang2002 commented 5 years ago

Hi, Using the latest downloaded chrome driver and chrom browser version 74.0.3729.131, the ashot does not take screenshot for selected webelements correctly, when tried on MacBook Pro

For example, I tried the google page attached. The google image, google search input and google search button are not specifcally or completely taken, as shown in attached zip file.

Kindly check if anyone see same thing? Anyany suggestion or work around are appreciated.

My code looks like this:

import java.io.File;
import java.io.IOException;
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.testng.annotations.Test;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;

public class AshotElementTaker 
{
    WebDriver driver;

    @Test
    public void imageComaparision() throws IOException
    {
        System.setProperty("webdriver.chrome.driver", "/Users/eugchang/Downloads/chromedriver");
        ChromeDriver driver = new ChromeDriver();

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

        //Google input
        WebElement search = driver.findElement(By.name("q"));

        //Google image
//        WebElement search = driver.findElement(By.xpath("//*[@id='hplogo']/a/img"));

        //Google Search button
//        WebElement search = driver.findElement(By.name("btnK"));

        Screenshot googleScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000))
        .takeScreenshot(driver,search);

        ImageIO.write(googleScreenshot.getImage(), "png",
                new File("/Users/eugchang/Downloads/googlescreenshot" + (int) (Math.random() * 100000) + ".png"));

        driver.quit();
    }
}
Google_Page

Google_webelements.zip

jfisher446 commented 4 years ago

I'm also unable to take a screenshot of an isolated WebElement. The whole page captures, instead.