js獲取元素的座標
阿新 • • 發佈:2020-12-29
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) # 座標就可以獲取到了