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
heightorwidthdirectly. I give two methods to getheightandwidth. Use each that matches your code.If there is some attribute that separates images from each other such as
id, then you can get imagewidthandheightusing.executeJavaScriptAndReturnValuemethod: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 :cssPathfunction 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)). functiongetArraytakes a selector sting and iterates throughquerySelectorAll()result, adding every element'swidth,heightand css path.Then call
getArrayfunction from java and parse the result:getArray. For that you can use calladdLoadListeneronbrowserand pass it aLoadAdapter. Then usingonFinishLoadingFramemethod of adapter.