简体   繁体   中英

Selenium WebDriver Shamelessly Returns Incorrect Height of WebElement

I'm trying to capture snaptshot of WebElement: .//*[@id='aplus']/div using Selenium Firefox WebDriver on this page: https://www.amazon.com/dp/B07G9RZD14

When I inspect element using Chrome I get the following dimensions: 979.2 by 1961.6

However, when calling element.getSize().getWidth() and element.getSize().getHeight() I get 979 (width) and 1461 (height). So the width matches the value retrieved by Chrome Inspector but the height is off by several hundred points (500 to be exact). WebDriver is returning the wrong height without being ashamed of it.

Moreover, when I take a screenshot of the actual element it seems that part of the height of the element is in fact not being captured so the value returned by Chrome seems to be the correct one.

I tried retrieving the "natural height and width" as suggested in this thread via getAttribute() and through JS but in both cases it just returns NULL .

Why is WebDriver returning the wrong height and how can I get it to return the correct value?

Thanks

It's because when you inspect the div, your browser has a fixed dimension, and when you run the test has a different one. Try to check the dimension in full screen mode, and run the test with the browser set to full screen as well.

I investigated this phenomenon further and it turns out that the size of the browser was NOT the cause of this problem. I always ran browser in maximized mode. It turns out there is an "expander" function in the HTML code that caused the element to grow after it is scrolled into view. How this works is the subject a new question I posted on SO.

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