def get_page_position(element):
    js = """
            var getAbsPosition = function(el){
            el = arguments[0]
            var el2 = el;
            var curtop = 0;
            var curleft = 0;
            if (document.getElementById || document.all) {
                do  {
                    curleft += el.offsetLeft-el.scrollLeft;
                    curtop += el.offsetTop-el.scrollTop;
                    el = el.offsetParent;
                    el2 = el2.parentNode;
                    while (el2 != el) {
                        curleft -= el2.scrollLeft;
                        curtop -= el2.scrollTop;
                        el2 = el2.parentNode;
                        }
                } while (el.offsetParent);

            } else if (document.layers) {
                curtop += el.y;
                curleft += el.x;
            }
        return [curtop, curleft];
        };
        return getAbsPosition(arguments[0])
    """
    print(driver.execute_script(js, element))
Ejemplo n.º 2
0
    def __exit__(self, *_):
        time.sleep(0.5)
        print("in __exit__")
        # WebDriverWait(self.browser, 20).until(staleness_of(self.old_page))
        wait_for(self.page_has_loaded, 0.5, 30)

        #这个只有page 加载的时候才能用
        s = "return document.readyState"
        wait_for(lambda: driver.execute_script(s) == "complete", 0.2, 60)
        print("__exit__")
def add_funtion_in_js(functionStr):
    """
    functionStr 格式如下 匿名函数
    function(canshu1,canshu2){

    }
    """
    js = """
                function addEvent_chen(obj, evType, fn) {
                    if (obj.addEventListener) {
                        obj.addEventListener(evType, fn, false);
                        return true;
                    } else if (obj.attachEvent) {
                        var r = obj.attachEvent("on" + evType, fn);
                        return r;
                    } else {

                    }
                }
                addEvent_chen(arguments[0],'click',%s);


            """ % (functionStr)
    driver.execute_script(js)
def checkClickable(liList):
    length = len(liList)
    clickAble = [1] * length
    for i in range(length):
        element = liList[i].find_element_by_tag_name("a")
        print(element.tag_name)
        print(element)
        if (element.is_displayed()):

            js = """
                function addEvent_chen(obj, evType, fn) {
                    if (obj.addEventListener) {
                        obj.addEventListener(evType, fn, false);
                        return true;
                    } else if (obj.attachEvent) {
                        var r = obj.attachEvent("on" + evType, fn);
                        return r;
                    } else {

                    }
                }
                addEvent_chen(arguments[0],'click',function(evt){
                    alert(evt.screenX +"   "+evt.clientX + evt.screenY +"   "+evt.clientY)
                });

            """
            # alert(evt.screenX +"   "+evt.clientX + evt.screenY +"   "+evt.clientY)
            driver.execute_script(js, element)
            element.click()
            # time.sleep(30)
            # element.click();
            # load_jquery(driver)
            #这边直接用jquery 不能点击 但是可以设置属性

            # js = "arguments[0].click(); arguments[0].dispatchEvent(evt);"
            js = "arguments[0].click();"
            # js = """
            #     var evt = evt = new MouseEvent('click',
            #         {
            #         canBubble:true, // canBubble
            #         cancelable:true, // cancelable
            #         screenX:%d, // screenX
            #         screenY:%d, // screenY
            #         clientX:%d, // clientX
            #         clientY:%d, // clientY
            #         ctrlKey:false, // ctrlKey
            #         altKey:false, // altKey
            #         shiftKey:false, // shiftKey
            #         metaKey:false, // metaKey
            #         button:0, // button
            #         relatedTarget:null //relatedTarget
            #         }
            #      );
            #     arguments[0].dispatchEvent(evt);
            #
            #     """ % (0,0,0,0)
            # driver.execute_script(js,element);

            classAttr = str(liList[i].get_attribute("class"))
            #代表没有找到 说明不能点击
            if (classAttr.find("tb-selected") == -1):
                clickAble[i] = 0
        else:
            clickAble[i] = 0

    return copy.deepcopy(clickAble)
Ejemplo n.º 5
0
def test_jquery():
    js = '$("body").css("background","red");'
    driver.execute_script(js)