from selenium importwebdriverfrom selenium.webdriver.support.ui importWebDriverWaitfrom selenium.webdriver.common.action_chains importActionChainsfrom PIL importImageimporttimeimportbase64importosimportioimportrandomclassJingdong():def __init__(self, username, password):
self.username=username
self.password=password
self.url= 'https://passport.jd.com/new/login.aspx?'self.options=webdriver.ChromeOptions()
self.options.add_experimental_option('excludeSwitches', ['enable-automation'])
self.browser= webdriver.Chrome(options=self.options, executable_path='D:\Program Files (x86)\chromedriver.exe')
self.wait= WebDriverWait(self.browser, 10)
self.browser.maximize_window()defopen_browser(self):
self.browser.get(self.url)
time.sleep(2)
self.browser.find_element_by_class_name('login-tab-r').click()
time.sleep(1)
self.browser.find_element_by_id('loginname').send_keys(self.username)
self.browser.find_element_by_id('nloginpwd').send_keys(self.password)
self.browser.find_element_by_class_name('login-btn').click()
time.sleep(1)defget_img_url(self):#获取验证码缺口图片的地址
img_url = self.browser.find_element_by_class_name('JDJRV-bigimg').find_element_by_css_selector('img').get_attribute('src')returnimg_url
@staticmethoddefget_image(image1):#将验证码的缺口图片与已有文件夹中的完整图片进行比较以取得其对应的完整图片
difference =0
image_=image1
size=image1.size
images= os.listdir('./img2')
pixel1=image1.load()for image inimages:
image= Image.open(f'./img2/{image}')
pixel2=image.load()
diff=0for j in range(size[1]):for i inrange(size[0]):
delta= abs(pixel1[i, j][0] - pixel2[i, j][0]) + abs(pixel1[i, j][1] - pixel2[i, j][1]) +abs(
pixel1[i, j][2] - pixel2[i, j][2])
diff+=deltaif difference ==0:
difference=diff
image_=imageelif difference >diff:
difference=diff
image_=imagereturnimage_
@staticmethoddefis_pixel_equal(image1, image2, x, y):#判断两张图片相同坐标的rgb数值是否近似
pixel1 =image1.load()[x, y]
pixel2=image2.load()[x, y]
threshold= 5
if abs(pixel1[0] - pixel2[0]) < threshold and abs(pixel1[1] - pixel2[1]) < threshold andabs(
pixel1[2] - pixel2[2])
left = 60img_url=self.get_img_url()
image1_bytes= base64.urlsafe_b64decode(img_url.split(',')[-1])
image1= Image.open(io.BytesIO(image1_bytes)).convert("RGB")
image2=self.get_image(image1)if notimage2:
self.browser.find_element_by_class_name('JDJRV-img-refresh').click()
time.sleep(1)
self.get_gap()else:for i inrange(left, image1.size[0]):for j in range(image1.size[1]):if notself.is_pixel_equal(image1, image2, i, j):returnireturnleft
@staticmethoddefget_track(distance):#轨迹生成
track =[]
track_=[]
delta_t= 0.1
for i in range(1, 21):
delta_dis= 1 / 12 * distance * (delta_t * i) ** 3 - 1 / 12 * distance * (delta_t * (i - 1)) ** 3track.append(round(delta_dis))for i in range(1, 10):
delta_dis= 1 / 3 * distance * (i * delta_t) ** 3 - 1 / 3 * distance * ((i - 1) * delta_t) ** 3track_.append(round(delta_dis))
track.append(0)while len(track_) >0:
track.append(track_.pop())
track[20] = distance -sum(track)returntrackdefverify(self):#滑动滑块进行验证
distince =self.get_gap()if distince > 180:
distince= distince - abs(180-distince)/180*40 - 40
else:
distince= distince + abs(180-distince)/180*40 - 40tracks=self.get_track(distince)
slider= self.browser.find_element_by_class_name('JDJRV-slide-btn')
ActionChains(self.browser).click_and_hold(slider).perform()for axis intracks:
yoffset= random.randint(-2, 2)
ActionChains(self.browser).move_by_offset(xoffset=axis, yoffset=yoffset).perform()
time.sleep(0.5)
ActionChains(self.browser).release().perform()
time.sleep(3)if self.browser.current_url !=self.url:print("登录成功")
time.sleep(3)
self.browser.close()else:
self.verify()defmain(self):
self.open_browser()
time.sleep(1)
self.verify()if __name__ == '__main__':
jingdong= Jingdong('username', 'password')
jingdong.main()