js获取元素的坐标
js_code = """ function getRect(elements) { var rect = elements.getBoundingClientRect(); w = rect.width || rect.right - rect.left; h = rect.height || rect.bottom - rect.top; ww = document.documentElement.clientWidth; hh = document.documentElement.clientHeight; return { top: (window.outerHeight - window.innerHeight) + Math.floor(rect.top), bottom: Math.floor(hh - rect.bottom), left: Math.floor(rect.left), right: Math.floor(ww - rect.right), width: ww, height: hh, elem_width: rect.width, elem_height: rect.height }; }; var box = arguments[0]; var obj = getRect(box); var str = obj.top + '|' + obj.left + '|' + obj.bottom + '|' + obj.right + '|' + obj.width + '|' + obj.height; return obj """ el = driver.find_element_by_xpath(xpath) res = driver.execute_script(js_code, el) # 坐标就可以获取到了
js_code = """functiongetRect(elements) {varrect = elements.getBoundingClientRect();w = rect.width || rect.right - rect.left;h = rect.height || rect.bottom - rect.top;ww = document.documentElement.clientWidth;hh = document.documentElement.clientHeight;return{top: (window.outerHeight - window.innerHeight) + Math.floor(rect.top),bottom: Math.floor(hh - rect.bottom),left: Math.floor(rect.left),right: Math.floor(ww - rect.right),width: ww,height: hh,elem_width: rect.width,elem_height: rect.height};};varbox = arguments[0];varobj = getRect(box);varstr = obj.top + '|' + obj.left + '|' + obj.bottom + '|' + obj.right + '|' + obj.width + '|' + obj.height;returnobj"""el = driver.find_element_by_xpath(xpath)res = driver.execute_script(js_code, el) # 坐标就可以获取到了