Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding of returned value of getLocation() Selenium WebDriver API

In Selenium WebDriver APIs, there is a method getLocation() which returns point, containing location of top left hand corner of the element. Let's say, it returns (x, y).

Is the point (x, y) with respective to Desktop's(browser's when it's in full screen mode) screen resolution or viewport's(The viewport, the visible area of the webpage within the browser window) resolution?

EDIT

Based on my answer to this question, I can guess getLocation() returns point with respective desktop's screen resolution or entire browser's(including toolbar, searchbar etc.) rather than viewport's. I just want to make sure that if my thinking is correct and if yes, why getLocation()'s behaviour is made like that? Is there any Selenium WebDriver method, which will return me point of element with respective to viewport?

Please correct if I'm wrong.

like image 532
Alpha Avatar asked Oct 20 '22 04:10

Alpha


2 Answers

The "exact place" on the page regardless of visibility.

This is why when doing image cropping if the element doesn't match the whole screen it will try to crop outside the screenshot as the actual element size/position extends beyond the visible screen.

However, if you are looking for the actual page properties and comparing this to the element size based on the location you can determine if a scrollbar should be present or not. element in the example below is an implementation of IWebElement as well as ICoordinates as the single IWebElement would not provide you with the ICoordinates interface as utilized below.

    Point p = element.LocationOnScreenOnceScrolledIntoView;
    OpenQA.Selenium.Interactions.Internal.ICoordinates p2 = element.Coordinates;

The above two offer a slight difference in capturing the location. The LocationOnScreenOnceScrolledIntoView one includes scrolling to the element and then would return the location based on the top left corner of the screen.

The Coordinates gives the specific location in various ways and includes the LocationInViewport property which would provide what you are looking for I believe.

    p = p2.LocationInViewport;
    p = p2.LocationOnScreen;
    p = p2.LocationInDom;
    object obj = p2.AuxiliaryLocator;
like image 101
mutt Avatar answered Oct 23 '22 01:10

mutt


In my understanding, it returns the point with respective Desktop's screen resolution or entire browser's(including toolbar, searchbar etc.), not viewpoint's.

Here is what I tried. In following code, I'm getting source element's location(which is point (x,y) of top left corner of the element) And moving mouse to that point. If you see, the mouse pointer isn't moved to the element's point rather it's moved little above of the element. This makes me to guess that getLocation() returns point of element with respective to desktop's screen resolution. Please carefully observe the mouse pointer before and after

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;

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.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class DragAndDropUsingRobot {
    WebDriver driver = null;

    @BeforeClass
    public void setUp(){
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("http://www.w3schools.com/html/html5_draganddrop.asp");
    }

    @AfterClass
    public void tearDown(){
        driver.quit();
    }

    @Test
    public void testDragAndDrop() throws AWTException, InterruptedException{
        WebElement source = driver.findElement(By.id("div1")); 

        Robot robot = new Robot();
        Thread.sleep(2000);
        robot.mouseMove(source.getLocation().getX(), source.getLocation().getY());

        Thread.sleep(5000);
    }
}
like image 26
Alpha Avatar answered Oct 23 '22 03:10

Alpha