--本方法通过滑动前后截屏,按图片相似度判断是否已滑动到底部
图片比较:
from PIL import Image
import numpy as np
import time
def cmppic(file1,file2):
img1,img2=Image.open(file1),Image.open(file2)
img1,img2=np.array(img1),np.array(img2)
return (img1==img2).mean()
滑动:
def scoll2bottom(driver,fun,start_x=500, end_x=505, start_y=1250, end_y=800):
isbottom=False
while not isbottom:
driver.save_screenshot('phone1.png')
fun() #每滑动一次进行回调
driver.swipe(start_x=start_x, end_x=end_x, start_y=start_y, end_y=end_y)
time.sleep(0.2)
driver.save_screenshot('phone2.png')
if cmppic('phone1.png','phone2.png')>0.97:
isbottom=True
在上面传进函数的回调函数中可以检测页面中所有所需的控件,然后把控件传进类似如下的函数里进行处理
顺便戴个判断元素是否处理过的帽子:
@checkiswatch
def seecourse(element):
path = 'course\%s' % element.text
try:
os.mkdir(path)
except:
pass
element.click()
time.sleep(2)
装饰器:
def checkiswatch(fun):
tempset=set()
def check(element,*b):
#print(tempset,element.text)
if element.text in tempset:
return False
else:
text=element.text
tempset.add(text)
rs=fun(element,*b)
if rs is False: #返回假则当没处理过,下次重新处理
tempset.remove(text)
return True
return check
--------------------------------------------