During automated testing, it is sometimes necessary to get the position and size of the element, to get the position of the element, to simulate the mouse with the actions, and to perform relative coordinates. For example, some elements are not easy to locate, or you need to take a brute-force click test of an element's relative area range, you need to get the coordinates.
Get the element location and size of the source code is relatively simple, directly on the code ... Please see!
1 /**2 * Get element position, and return integer Array [left, top, width, height]3 * 4 * @authorAARON.FFP5 * @versionV1.0.0:autoseleniumdemo main.aaron.sele.core Seleniumcore.java getelementpositionandsize, 2015-7-27 23:38:31 Exp $6 * 7 * @paramWebdriver:webdriver8 * @paramBy:by9 * Ten * @returnInt[left,top,width,height] One */ A Public int[] getelementpositionandsize (Webdriver webdriver, by) { - //Store Element Position - int[] Elementposition =New int[4]; the - //get Element -webelement element =webdriver.findelement (by); - + //get location of element -Point e_location =element.getlocation (); + A //get size of element atDimension e_size =element.getsize (); - -Elementposition[0] =e_location.getx (); -ELEMENTPOSITION[1] =e_location.gety (); -ELEMENTPOSITION[2] =E_size.width; -ELEMENTPOSITION[3] =E_size.height; in - returnelementposition; to } + - /** the * Get element position, and return integer Array [left, top, width, height] * * $ * @authorAARON.FFPPanax Notoginseng * @versionV1.0.0:autoseleniumdemo main.aaron.sele.core Seleniumcore.java getelementpositionandsize, 2015-7-27 23:35:31 Exp $ - * the * @paramBy:by + * A * @returnInt[left,top,width,height] the */ + Public int[] Getelementpositionandsize (by) { - //Store Element Position $ int[] Elementposition =New int[4]; $ - //get Element -webelement element = This. webdriver.findelement (by); the - //get location of elementWuyiPoint e_location =element.getlocation (); the - //get size of element WuDimension e_size =element.getsize (); - AboutElementposition[0] =e_location.getx (); $ELEMENTPOSITION[1] =e_location.gety (); -ELEMENTPOSITION[2] =E_size.width; -ELEMENTPOSITION[3] =E_size.height; - A returnelementposition; +}
At this point, theWebUI automation function test Script No. 028-Get the element location and size of the smooth end, I hope this article can give beginners Selenium you a reference.
Finally, very grateful to the pro-stop, I hope this article can be pro helpful. Warmly welcome the kiss to discuss together and progress together. Thank you so much! ^_^
SELENIUM2 Learning -030-webui Automation Practical instance -028-get the element position and size