简体   繁体   English

Selenium WebDriver 无耻地返回错误的 WebElement 高度

[英]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我试图WebElement捕获snaptshot: .//*[@id='aplus']/div使用火狐硒WebDriver这个网页上: https://www.amazon.com/dp/B07G9RZD14

When I inspect element using Chrome I get the following dimensions: 979.2 by 1961.6当我使用 Chrome 检查元素时,我得到以下尺寸: 979.2 by 1961.6

However, when calling element.getSize().getWidth() and element.getSize().getHeight() I get 979 (width) and 1461 (height).但是,当调用element.getSize().getWidth()element.getSize().getHeight()我得到979 (宽度)和1461 (高度)。 So the width matches the value retrieved by Chrome Inspector but the height is off by several hundred points (500 to be exact).因此宽度与Chrome Inspector检索到的值相匹配,但高度相差数百个点(准确地说是 500)。 WebDriver is returning the wrong height without being ashamed of it. WebDriver 返回错误的高度,但并不为此感到羞耻。

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.此外,当我截取实际元素的屏幕截图时,似乎元素的部分高度实际上并未被捕获,因此 Chrome 返回的值似乎是正确的。

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 .我尝试通过getAttribute()和通过JS检索该线程中建议的“自然高度和宽度”,但在这两种情况下,它都只返回NULL

Why is WebDriver returning the wrong height and how can I get it to return the correct value?为什么 WebDriver 返回错误的高度,我怎样才能让它返回正确的值?

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.这是因为当你检查 div 时,你的浏览器有一个固定的维度,而当你运行测试时有一个不同的维度。 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.事实证明,HTML 代码中有一个“扩展器”函数,导致元素在滚动到视图中后增长。 How this works is the subject a new question I posted on SO.这是如何工作的主题是我在 SO 上发布的一个新问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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