appm

#!/bin/env python
#-*- encoding=utf8 -*-

import random,os,sys
import ip_cookie
from time import *
from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 获取当前函数所在目录的绝对路径
def get_path():
    return os.path.split(os.path.realpath(__file__))[0]

# sleep一定时间(时长随机)
def random_sleep(min,max):
    time=round(random.uniform(min,max),1)
    sleep(time)

# 等待元素出现
def wait_element_OK(driver,locator,by='xpath'):
    if by=='xpath':
        WebDriverWait(driver,6).until(EC.visibility_of_element_located((By.XPATH,locator)))
        WebDriverWait(driver,7).until(EC.element_to_be_clickable((By.XPATH,locator)))
    if by=='id':
        WebDriverWait(driver,6).until(EC.visibility_of_element_located((By.ID,locator)))
        WebDriverWait(driver,7).until(EC.element_to_be_clickable((By.ID,locator)))
    if by=='class':
        WebDriverWait(driver,6).until(EC.visibility_of_element_located((By.CLASS_NAME,locator)))
        WebDriverWait(driver,7).until(EC.element_to_be_clickable((By.CLASS_NAME,locator)))
    if by=='text':
        WebDriverWait(driver,6).until(EC.visibility_of_element_located((By.LINK_TEXT,locator)))
        WebDriverWait(driver,7).until(EC.element_to_be_clickable((By.LINK_TEXT,locator)))

# 点击元素
def click_element(driver,locator,by='xpath'):
    if by=='xpath':
        WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,locator)))
        driver.find_element_by_xpath(locator).click()
    if by=='id':
        WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.ID,locator)))
        driver.find_element_by_id(locator).click()
    if by=='class':
        WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.CLASS_NAME,locator)))
        driver.find_element_by_class_name(locator).click()
    if by=='text':
        WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.LINK_TEXT,locator)))
        driver.find_element_by_link_text(locator).click()
    random_sleep(1,3)

# 输入文本
def input_text(driver,text,locator,by=By.XPATH):
    try:
        WebDriverWait(driver,6).until(EC.visibility_of_element_located((by,locator)))
        WebDriverWait(driver,7).until(EC.element_to_be_clickable((by,locator)))
        driver.find_element(by=by, value=locator).clear()
        driver.find_element(by=by, value=locator).send_keys(text)
        random_sleep(1,3)
    except Exception,e:
        print u'输入失败!locator:%s'%locator
        time=strftime('%m%d_%H%M',localtime())
        driver.get_screenshot_as_file(get_path()+'/error_png/%s.png'%time)
        raise

# 获取元素指定属性值
def get_attr(driver,attr,locator,by=By.XPATH):
    try:
        WebDriverWait(driver,6).until(EC.visibility_of_element_located((by,locator)))
        WebDriverWait(driver,7).until(EC.element_to_be_clickable((by,locator)))
        driver.find_element(by=by, value=locator).get_attribute(attr)
        random_sleep(1,3)
    except Exception,e:
        print e
        print u'获取属性值失败!locator:%s'%locator
        time=strftime('%m%d_%H%M',localtime())
        driver.get_screenshot_as_file(get_path()+'/error_png/%s.png'%time)

# 打开浏览器
def open_browser(url,driver='Chrome',iport=None,cookie=None):

    if iport is not None:
        ip=iport.split(':')[0]
        port=iport.split(':')[1]
        if driver=='Chrome':
            option=ip_cookie.chrome_proxy_options(ip,port)
            driver=webdriver.Chrome(chrome_options=option)
        if driver=='Firefox':
            profile=ip_cookie.firefox_proxy_options(ip,port)
            driver=webdriver.Firefox(firefox_profile=profile)

    if cookie is not None:
        pass

    if iport==None and cookie==None:
        if driver=='Chrome':
            driver=webdriver.Chrome()
        if driver=='Firefox':
            driver=webdriver.Firefox()
        if driver=='Ie':
            driver=webdriver.Ie()
            
    driver.maximize_window()
    driver.get(url)
    return driver

# 首页随意滚动鼠标滚轮
def homepage_scrolling(bs): y_position=random.randint(310,380) max_position=random.randint(3300,5300) # 下滑 while y_position<max_position: bs.execute_script('window.scrollTo(0,%d)'%y_position) y_position+=random.randint(330,390) random_sleep(0.5,2) # 上滑 while y_position>0: bs.execute_script('window.scrollTo(0,%d)'%y_position) y_position-=random.randint(530,690) random_sleep(0.5,1) bs.execute_script('window.scrollTo(0,%d)'%0)# 回到页面顶部

 

posted @ 2018-06-16 17:27  天际流云  阅读(686)  评论(0编辑  收藏  举报