selenium常见问题:element is not attached to the page document,页面刷新后元素失效,需要重新定位元素

 测试中,常遇到此问题,特此写一个重新获取元素的函数,已供调用解决此方法

#找不到元素时,重新获取元素,eletype默认获取单个元素,若=1,则获取多个元素
#weblement有值,则传入webelement
def retying_get_element(self,code,eletype=0,webelement=None):
    count = 0
    ele = None
    while count <= 10:
        count += 1
        time.sleep(1)

        if eletype==1:
            if webelement == None:
                try:
                    ele = CuoTiSelect.dr.find_elements_by_css_selector(code)
                    for one in ele:
                        if one.get_attribute('outerHTML')!='':
                            break
                        else:
                            continue
                except:
                    pprint(u'没有找到element')

            elif webelement is not None:
                try:
                    ele =webelement.find_elements_by_css_selector(code)
                    for one in ele:
                        if one.get_attribute('outerHTML') != '':
                            break
                        else:
                            continue
                except:
                    pprint(u'没有找到element')
        elif eletype == 0:
            try:
                ele = CuoTiSelect.dr.find_element_by_css_selector(code)
                if ele.get_attribute('outerHTML') != '':
                    break

            except:

                pprint(u'没有找到element')



    return ele

猜你喜欢

转载自blog.csdn.net/qq_35958094/article/details/81663065