How can I get the clientwidth and clientheight if I have a list of img
imgs = document.findElements(By.cssSelector("a>img"));
for(DOMElemnt img:imgs){
double height =img.getAttribute("height");//not always works ,sometime return null value
//What API can I use?
}
Or should I use jquery? but how can it combined with jxbrowser API? Thanks!
As far as I know, there is no API for getting an element's
height
orwidth
directly. I give two methods to getheight
andwidth
. Use each that matches your code.If there is some attribute that separates images from each other such as
id
, then you can get imagewidth
andheight
using.executeJavaScriptAndReturnValue
method:If images do not have such attribute to separate them, you can call a function in javascript which returns an array of elements and every element consists of css path, width and height. By doing so, you can access images width and height directly. Also the css path can be used to access
DOMElement
. First of all, add this script to your html :cssPath
function takes an element and returns css path string. The code is just copied from here (asselin's answer) with a little modification(replacedif (nth != 1)
withif (el.previousElementSibling != null || el.nextElementSibling != null)
). functiongetArray
takes a selector sting and iterates throughquerySelectorAll()
result, adding every element'swidth
,height
and css path.Then call
getArray
function from java and parse the result:getArray
. For that you can use calladdLoadListener
onbrowser
and pass it aLoadAdapter
. Then usingonFinishLoadingFrame
method of adapter.