简体   繁体   中英

Checking position of elements in Selenium WebDriver using Java

I would like to check the position of the elements on a web application on which I am working on. It's a stable application so the positions won't change so much. After searching on the internet I've found this method:

Point getLocation()

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

Returns: A point, containing the location of the top left-hand corner of the element

After playing a little bit with it I realized that the position depends on the window size and screen resolution (though I read it somewhere I still checked it just to make sure it's true).

Is there another way? I have a lot of elements so it will be tricky to just store some values depending on a set of resolutions and window dimensions.That would be a lot of work.

In the end nothing beats manual testing when it comes to visual stuff :D

It has to depend on the window size, because the layout of the page changes with the window size.

What you're asking for is essentially:

Can I get the position of elements on the page as if the window was infinitely wide and infinitely high so that the result would be static?

No, sorry, you can't do that, because you're probably the first person wanting to do such thing. The positions you're asking for are meaningless. Also, these numbers will most likely be different across the browsers.

I believe you have two options:

  1. Set a fixed window size for this test. If you'll run the test at 1024*768 px every time, it usually won't break. In this case, your positions shouldn't change and you'll get your results.
    • Bonus idea: you can get a screenshot of the page and compare the images. If the page's content is more or less static, your screenshot will always be the same, so you can compare it to a reference screenshot.
  2. Don't check the absolute numbers, try to check for example whether a WebElement contains the right text, images (and whether the images have been loaded) or even whether a WebElement is visually contained in another WebElement .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM