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) # 坐标就可以获取到了
posted @ 2020-12-29 10:54  雪丶寒  阅读(494)  评论(0)    收藏  举报