Skip to content
This repository has been archived by the owner on Nov 29, 2018. It is now read-only.

WebElement.getLocation and WebElement.getSize in screen shots #2525

Closed
lukeis opened this issue Mar 3, 2016 · 7 comments
Closed

WebElement.getLocation and WebElement.getSize in screen shots #2525

lukeis opened this issue Mar 3, 2016 · 7 comments

Comments

@lukeis
Copy link
Member

lukeis commented Mar 3, 2016

Originally reported on Google Code with ID 2525

Hi!

I am trying to use Selenium to mark the position of elements in a screen shot taken
with Selenium. The steps I take are as follows:

- Start the Webdriver and navigate to a URL.
- Take a screen shot of the page.
- Use WebDriver.findElements to identify block level elements (divs) in the page.
- Use WebElement.getLocation and WebElement.getSize to get the area where each element
is located in the screen shot.
- Draw the corresponding rectangle at the extracted coordinates in the screen shot.

I'm trying this for IE, Firefox and Chrome. For some browsers, I get correctly sized
rectangles at a slightly different position, but the offset is constant. However, in
Chrome 13 on Windows for example the rectangles are to small and sometimes (and that's
really a problem) and the wrong position.

My question is: Is this a known problem? I searched the issue tracker and asked at
the mailing list, but didn't get a reply. It seems that the Webdriver uses Javascript
to get the coordinates, which may be a problem. What's really weird is that it sometimes
works and sometimes doesn't.

My setup is as follows:

Selenium version: 2.5.0
OS: Linux (Hub), Windows (Browser)
Browser: Chrome 
Brower version: 13

I use a RemoteWebDriver running on Linux to connect to a selenium server running on
Windows, which launches Chrome. 

To further investigate the problem, I am using Javascript to change the background
color of the divs I'm interested in and take a screen shot again. For some browsers,
there is a severe mismatch between the rectangle returned by getLocation and getSize
and the actual position where the element is rendered by the browser.

Maybe this has something to do with using screen shots and getLocation/getSize together?
I would settle for anything that gives me consistent (maybe offset) results for anything.

Thanks in advance for your help/comments.

Valentin

Please provide any additional information below. A sample reduced test
case, or a public URL that demonstrates the problem will intrigue our merry
band of Open Source developers far more than nothing at all: they'll be far
more likely to look at your problem if you make it easy for them!

Reported by valentin.dallmeier on 2011-09-22 12:42:12

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

Here is an example URL where things go wrong:

http://www.martin-burger.net/en/

For element <div class="subcl"> "Co-founder and partner of the civil law partnership
netbeyond", I get an area of x=372, y=510, width=291, height=532, which is not the
correct size and position.

Reported by valentin.dallmeier on 2011-09-22 12:45:46

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

Does:

((JavascriptExecutor)driver).executeScript("return arguments[0].getBoundingClientRect()[\"width\"]")
give the correct values? (for width, height, left, top, ...)?

Reported by dawagner on 2011-09-23 15:41:58

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

Actually, the coordinates as given by WebElement.getLocation() and WebElement.getSize()
are correct. However, not all screenshots represent the *actual* viewport (the one
in the visible browser window as shown on the desktop), but a smaller one (most likely,
the methods used to take a screenshot cause the browser to re-render the page in a
different viewport).

A detailed diagnosis can be found in Issue #2536 "Screenshot given by Selenium does
not match actual viewport (Google Chrome)".

Please note that this issue does not occur in Google Chrome only, but in other browser
as well (as mentioned by Valentin).

Reported by mburger313 on 2011-09-25 17:52:24

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

Issue #2536 contains a much better diagnosis of the problem. For the sake of completeness,
I tried several different approaches to get the coordinates, including offset(), width()
and height() of jQuery 1.6.4 which returns exactly the same coordinates for the case
described in issue #2536.

Reported by valentin.dallmeier on 2011-09-26 09:22:58

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

Reported by barancev on 2011-10-13 09:10:45

  • Labels added: Component-WebDriver

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

Reported by barancev on 2012-12-08 20:27:15

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

Reported by luke.semerau on 2015-09-17 18:13:46

  • Labels added: Restrict-AddIssueComment-Commit

@lukeis lukeis closed this as completed Mar 3, 2016
@SeleniumHQ SeleniumHQ locked and limited conversation to collaborators Mar 4, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant