self.driver webdriver.Remote(DRIVER_SERVER, self.desired_caps)
self.wait WebDriverWait(self.driver, TIMEOUT)
self.hours_en 60 * 60 * 1.1 # en控制1.1小时模拟点击一次
self.date_start_en time.time() # en开始时间
self.date_end_en 0 # en超过此时间后再次运行
# self.date_end_en self.date_start_en self.hours_en # en超过此时间后再次运行
self.week 60 * 60 * 24 * 7 # 按照周的频率对xd进行token更新
self.week_start_xd time.time() # xd的开始时间
self.week_end_xd 0 # 根据周控制频率控制再次开启时间
self.week_start_xiu time.time() # xd的开始时间
self.week_end_xiu 0 # 根据周控制频率控制再次开启时间
def login(self):
登录微信
:return:
# 登录按钮
a time.time()
try:
login self.wait.until(EC.presence_of_element_located((By.ID, com.tencent.mm:id/f34 )))
login.click()
except Exception as e:
# print(e)
logging.info(f failed login {e} )
b time.time() - a
# print( 点击登录 , b)
logging.info(f click login,use time {b} )
# 手机输入
try:
phone self.wait.until(EC.presence_of_element_located((By.ID, com.tencent.mm:id/bem )))
phone.set_text(USERNAME)
except Exception as e:
# print(e)
logging.info(f something wrong{e} )
c time.time() - a - b
# print( 手机号输入 , c)
logging.info(f send keys phone nums use time {c} )
# 下一步
try:
next self.wait.until(EC.element_to_be_clickable((By.ID, com.tencent.mm:id/dw1 )))
next.click()
except Exception as e:
logging.info(f something wrong{e} )
d time.time() - a - b - c
logging.info(f click next bottom use time {c} )
# 密码
password self.wait.until(EC.presence_of_element_located((By.XPATH, //*[ text 请填写微信密码 ] )))
password.set_text(PASSWORD)
e time.time() - a - b - c - d
logging.info(f send keys password use time {e} )
# 提交
# submit self.wait.until(EC.element_to_be_clickable((By.ID, com.tencent.mm:id/dw1 )))
submit self.wait.until(EC.element_to_be_clickable((By.XPATH, //*[ text 登录 ] )))
submit.click()
f time.time() - a - b - c - d - e
logging.info(f commit password use time {f} )
def run(self):
:return:
# 滑动之后等待出现en小程序
self.slide_down()
time.sleep(10)
# 点击进入en小程序
self.touch_en()
if self.week_end_xd self.week_start_xd:
self.week_start_xd time.time()
self.week_end_xd self.week_start_xd self.week
print( xd点击 )
self.touch_xd()
elif self.week_end_xiu self.week_start_xiu:
self.week_end_xiu time.time() self.week
print( xiu )
self.touch_xiu()
time.sleep(10)
# 退出小程序
self.driver_closed()
print( driver closed )
emt EnToken()
token_res emt.token_2_redis()
if not token_res:
print( 需要发送失败消息 )
return False
return True
def slide_down(self):
滑动微信屏幕之后点击小程序
:return:
window_size_phone self.driver.get_window_size()
# print(window_size_phone)
phone_width window_size_phone.get( width )
phone_height window_size_phone.get( height )
# print(phone_width, phone_height)
time.sleep(15)
x1 phone_width * 0.5
y1 phone_height * 0.7
y2 phone_height * 0.26
# print( 准备向下滑动 )
logging.info(f prepare slide down )
a time.time()
self.driver.swipe(x1, y2, x1, y1, 2050)
# print( 向下滑动完成 , time.time() - a)
logging.info(f slide down success use time {time.time() - a} )
def touch_en(self):
每次进来之后都需要判断是否到了时间 若时间到了之后才可执行点击操作
:param : en 代表en; xd 代表xd; xiu 代表xiu.
:return: None 无返回值
print(self.date_end_en, time.time())
if self.date_end_en time.time(): # 此时的时候已经超时 需要再次从新进行点击
print( en模拟点击 )
# 从新定义开始结束时间
print(self.date_end_en, time.time())
self.date_end_en time.time() self.hours_en # 再次更改end time为n小时后
print(self.date_end_en, time.time())
try:
# print( id定位en )
en_app self.wait.until(
EC.presence_of_element_located((By.XPATH, f //android.widget.TextView[ text textname… ] )))
# en_master self.wait.until(EC.presence_of_element_located((By.ID, com.tencent.mm:id/hu )))
# en_master self.wait.until(
# EC.presence_of_element_located((By.XPATH, //android.widget.TextView[ text textname ] )))
en_app.click()
logging.info(f located by app_name en )
except Exception as error:
# print(e, id定位失败 )
logging.info(f failed located by id:{error} )
time.sleep(20)
# 关闭小程序按钮点击
print( close the en app )
close_button self.wait.until(EC.presence_of_element_located((By.XPATH, f //android.widget.frameLayout[2]/android.widget.ImageButton )))
close_button.click()
print( 点击了关闭小程序 )
def touch_xd(self):
需要考虑是否已经登录状态还是需要再次登录
:return:
# 点击后进入到小程序
logging.info( click app xd )
xd_app self.wait.until(EC.presence_of_element_located((By.XPATH, //android.widget.TextView[ text textname ] )))
xd_app.click()
time.sleep(20)
# 页面出现需要获取到你的定位的时候需要点击允许
print( 点击确认获取当前位置 )
self.driver.tap([(510, 679)], 500)
# 点击进入到个人中心
time.sleep(10)
logging.info( click personal xd )
self.driver.tap([(540, 1154)], 500)
# 点击快速登录进行登录
time.sleep(10)
logging.info( click login xd )
self.driver.tap([(270, 1030)], 500)
# 点击同意获取头像信息
time.sleep(10)
logging.info( 同意获取头像等相关信息 )
self.driver.tap([(510, 775)], 500)
time.sleep(20)
# 关闭小程序按钮点击
print( close the guaishou app )
close_button self.wait.until(
EC.presence_of_element_located((By.XPATH, f //android.widget.frameLayout[2]/android.widget.ImageButton )))
close_button.click()
print( 结束 )
time.sleep(30)
def touch_xiu(self):
xiu模拟点击 需要考虑是否需要登录状态下
:return:
# 点击后进入到小程序
logging.info( click app xiu )
xiu_app self.wait.until(EC.presence_of_element_located((By.XPATH, //android.widget.TextView[ text xiu ] )))
xiu_app.click()
# 若页面显示需要确认获取当前位置的话需要点击确认
logging.info( click confirm xiu )
time.sleep(15)
/confirm/i_loc self.wait.until(
EC.presence_of_element_located((By.XPATH, //android.widget.Button[ text 确定 ] )))
/confirm/i_loc.click()
# 点击个人中心
logging.info( click personal xiu )
time.sleep(5)
try:
personal self.wait.until(
EC.presence_of_element_located((By.XPATH, //android.view.View[ content-desc 个人中心 ] )))
personal.click()
except Exception as e:
print(e)
# 点击快速登录进行登录
logging.info( click login xiu )
time.sleep(5)
try:
login self.wait.until(EC.presence_of_element_located((By.XPATH, //android.view.View[ content-desc 立即登录 ] )))
login.click()
except Exception as e:
print( xiu已经登录 不需要再次点击确认登录 )
time.sleep(30)
def driver_closed(self):
self.driver.quit()