Repository navigation
OffsetHeight not calculated properly #124
Description
Activity
Thanks for the report. Will have a look and try to improve the implementation. But keep in mind HtmlUnit is a headless renderer, there is no layout engine inside.
Thanks @rbri - what exactly does that mean? When HtmlUnit renders my page, it does seem like it takes into account the styles I provide in my CSS. Maybe I'm just inferring that, but are you saying it doesn't evaluate the CSS?
The reason I'm slightly confused is because when I do a similar task to the above code using the offsetWidth property, it does function exactly like I want it to.
When HtmlUnit renders my page
HtmlUnit never renders your page. HtmlUnit is headless - there is no rendering step. The whole processing is done on the dom. But we try to simulate also some of the layout related properties.
In your case HtmlUnit will use only the font size to calculate the offsetHeight, but will NOT do any line breaks to fit into the box.
Is this your problem?
That's exactly right- I was expecting the line breaks to be taken into account, but if they're not, it makes sense. Thanks.
@rbri Still dealing with this issue and wondering if there is any workaround you can provide. Essentially I have a
Have spent some more days on this, now we do word wrapping.
Some other minor fixes are also done.
Of course you still not get the correct values because we do no rendering but the height is now adjusted in every loop.
Will make a new snapshot build available as soon as all the unit tests have passed (check twitter).
Hope that helps.
Wow! That's wonderful! I'll keep an eye out for the new release. Thank you so much for your time and energy on this- it will really help us out!
A new snapshot build is available - please try and provide feedback.
@rbri What's the version number that I should include in my gradle build? I don't see any such releases on the github page or the website.
for maven you need
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.44.0-SNAPSHOT</version>
</dependency>
and maybe
<snapshotRepository>
<id>sonatype-nexus-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
I guess you are able to translate this into gradle :-)
Hi @rbri - I am able to import the snapshot properly, but I see there is a new "parse" method to parse an HTML string onto the page. Would you mind providing a code snippet for how to implement this? I can only find the past version on your FAQ using the parseHtml and parseXhtml methods.
I did attempt to use the new parse method, but I'm getting this error: java.lang.NoSuchMethodError: 'void com.gargoylesoftware.htmlunit.html.parser.HTMLParser.parse(com.gargoylesoftware.htmlunit.WebResponse, com.gargoylesoftware.htmlunit.html.HtmlPage, boolean)'
Looks like the HtmlUnitNekoHtmlParser from version 2.40.0 is being used somehow, which is why the parse method is not found. Not sure what the fix is, but that's the cause.
Maybe you have different versions of an lib in your classpath.
Nearly impossible - do you have an (old) htmlunit driver also in your project?
Please try to have a look at the dependency tree (https://docs.gradle.org/current/userguide/viewing_debugging_dependencies.html)
I was able to figure out how to exclude the older version from library imports. However, once I do that, I get error messages like this:
java.lang.ClassNotFoundException: com.gargoylesoftware.htmlunit.AlertHandler
and
java.lang.ClassNotFoundException: com.gargoylesoftware.htmlunit.WebResponse
So it's either those errors, or if I do let the project include version 2.40.0 like in the screenshot above, then I still get this:
java.lang.NoSuchMethodError: 'void com.gargoylesoftware.htmlunit.html.parser.HTMLParser.parse(com.gargoylesoftware.htmlunit.WebResponse, com.gargoylesoftware.htmlunit.html.HtmlPage, boolean)'
Please make sure you only have the lastest versions of all the dependent lib in the classpath
<htmlunitcssparser.version>1.5.0</htmlunitcssparser.version>
<htmlunitneko.version>2.44.0-SNAPSHOT</htmlunitneko.version>
<htmlunitcorejs.version>2.43.0</htmlunitcorejs.version>
@rbri I was able to get the classpath issues fixed, and it looks like the new updates are working great! Thank you so much for you work on this, it's really wonderful. Any idea when 2.44.0 will be released?
Thanks for the feedback - enjoy using HtmlUnit
@rbri After updating to the new 2.44.0 release, I'm having problems parsing HTML. Here's what I had prior to this update:
val client = new WebClient()
val response = new StringWebResponse(html, new URL(s"file:$resourcePath/static/css"))
val page = client.getPageCreator.getHtmlParser.parseHtml(response, client.getCurrentWindow)
and here's what I've been trying with the new release, since parseHtml method doesn't exist anymore:
val client = new WebClient()
val response = new StringWebResponse(html, new URL(s"file:$resourcePath/static/css"))
val page = new XHtmlPage(response, client.getCurrentWindow)
client.getPageCreator.getHtmlParser.parse(response, page, true)
I get this error thrown at the last line: java.lang.IllegalStateException: No script object associated with the Page. class: 'com.gargoylesoftware.htmlunit.html.XHtmlPage' url: 'file:../sign-templates/src/main/resources/static/css' content: <!DOCTYPE > .......
I even tried adding this before the final line: page.setScriptableObject(client.getCurrentWindow.getScriptableObject)
Could you please provide an updated example of how to parse HTML to the page?
Sorry for breaking the backward compatibility. Have updated the FAQ to make the sample code working again.
Perfect, thanks so much! And thank you for all the work you do on the library!
Hi @rbri -
After upgrading to 2.44.0 and attempting to calculate the offsetHeight on an element, I am still not seeing a value that takes text wrapping into account. Should I be seeing it in this case? When I tested with the snapshot, it did work correctly.
Sorry but there is no change regarding this between the snapshots and the release. Strange...
Please reopen if you still have this problem.

Running version 2.36.0.
When I build an HTMLUnit page using the following HTML, the offsetHeight is not properly calculated. It resolves to the height of the inner "title-sizer" element, rather than the full height rendered by the long text in the "title" element.
This code works correctly in the Chrome browser when opened from an .html file.