def get_element_attribute(element,att): loginfo(u'开始获取元素中的属性') try: result=element.get_attribute(att) loginfo(u'元素属性:%s=%s'%(att,result)) return result except: return 0
def verify_element(driver,method,para,times=3): loginfo(u'[%s]开始查找元素:%s'%(method,para)) ret=False for i in range(times): sleep(1) ret=verify_element_(driver, method, para,i==times-1) if ret: break return ret
def login_cookie(driver, leap_cookies): loginfo(u'%s注入cookie%s\n' % ('+' * 12, '+' * 12)) home_url = (driver.current_url).split("/#/") + "/#/home" driver.get(home_url) driver.add_cookie(leap_cookies) sleep(2) driver.get(home_url) sleep(3) loginfo(u"Cookie 注入完成")
def get_element_text(driver,method,para): ele=find_elements(driver, method, para) if ele and len(ele)==1: try: loginfo(u'[%s]开始获取元素:%s中的文本'%(method,para)) result=ele[0].text return result except Exception,e: logerror(u'[%s]获取元素:%s中的文本失败\n%s' %(method,para,e)) return None
def click_element(driver,method,para): ele=find_elements(driver, method, para) if ele and len(ele)==1: try: loginfo(u'[%s]开始点击元素:%s' %(method,para)) ele[0].click() return True except Exception,e: logerror(u'[%s]点击元素:%s失败\n%s' %(method,para,e)) return False
def input_text(driver,method,para,text): ele=find_elements(driver, method, para) if ele and len(ele)==1: try: loginfo(u'[%s]开始向元素:%s中输入字符:%s' %(method,para,text)) ele[0].clear() ele[0].send_keys(text) return True except Exception,e: logerror(u'[%s]向元素:%s输入字符失败\n%s' %(method,para,e)) return False
def shell_command(command, info="", prin_t=True): loginfo(command) cm = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) _, err = cm.communicate() if cm.returncode != 0: logerror(err) logerror("Non zero exit code:%s executing: %s" % (cm.returncode, info)) raise StandardError
def login(driver): loginfo(u'%s登录%s\n' % ('+' * 12, '+' * 12)) input_text(driver, By.CSS_SELECTOR, '#username', 'admin') input_text(driver, By.CSS_SELECTOR, '#password', 'admin') # click_element(driver,By.CSS_SELECTOR,".btn-primary") driver.execute_script("$('.btn-primary').click()") sleep(3) # loginfo("cookie: "+str(get_cookies(driver))) current_url = driver.current_url login_url = current_url.split(":7180")[0] + ":7180/cmf/login" loginfo(u'当前页面:' + str(current_url)) bflag = str(current_url) == login_url return FuncResult(not bflag, u"登录", caputre(driver))