Example usage for org.openqa.selenium WebElement getLocation

List of usage examples for org.openqa.selenium WebElement getLocation

Introduction

In this page you can find the example usage for org.openqa.selenium WebElement getLocation.

Prototype

Point getLocation();

Source Link

Document

Where on the page is the top left-hand corner of the rendered element?

Usage

From source file:UI_Manipulation.SeleniumUtils.java

/**
* Method to move to an element that is not in sight and click on it
* @param element WebElement to find/*from   ww  w  . ja  v a  2  s.  c o  m*/
* @param click If true, then click on element after find it
* @throws Exception 
*/
public void MoveToElementThatIsNotInSight(WebElement element, boolean click) throws Exception {
    try {

        String js = "window.scrollTo(0, " + (element.getLocation().y - 50) + ")";
        JavascriptExecutor executor = (JavascriptExecutor) driver;

        executor.executeScript(js);

        Thread.sleep(500);

        if (click) {
            new Actions(driver).click(element).build().perform();
        }
    } catch (Exception ex) {
        if (ex.getMessage().contains("Element is not clickable at point")) {
            String js = String.format("window.scrollBy(0, {0});)", -50);
            JavascriptExecutor executor = (JavascriptExecutor) driver;

            executor.executeScript(js);
            new Actions(driver).click(element).build().perform();
        }
        throw new Exception(
                "Error when trying to MoveToElement to make it visible and click on it after! More Details:"
                        + ex.getMessage());
    }
}

From source file:zz.pseas.ghost.utils.BrowserUtil.java

License:Apache License

public static byte[] captureScreenShotById(WebDriver driver, String id) throws IOException {
    byte[] bytes = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
    WebElement ele = driver.findElement(By.id(id));
    ByteArrayInputStream in = new ByteArrayInputStream(bytes);

    BufferedImage image = ImgUtil.bytesToBImage(bytes);

    ImageIO.write(image, "jpg", new File("d:/big.jpg"));
    in.close();// w w w.  j a va 2s.  c om
    System.out.println(image.getType());

    Point p = ele.getLocation();
    Dimension xy = ele.getSize();

    BufferedImage subImage = image.getSubimage(p.getX(), p.getY(), xy.getWidth(), xy.getHeight());
    ImageIO.write(subImage, "jpg", new File("d:/sc.jpg"));
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    ImageIO.write(subImage, "jpg", out);
    out.flush();
    byte[] res = out.toByteArray();
    out.close();
    return res;
}