Rumah >pembangunan bahagian belakang >Tutorial Python >selenium之实现自动登录的实例代码
使用selenium及打码平台(自己还实现不了验证码图片解析,对接打码平台解析图片验证码)现实自动登录亚马逊网站,并修改账号绑定邮箱及密码等操作。
逻辑:仿真浏览器的操作,定位元素填入数据,根据页面元素获取属性下载图片验证码进行分析,自动填充验证码,实现自动化操作。
使用chrome浏览器,需要下载对应版本的webdriver 下载地址:。
#coding=utf-8from selenium import webdriverfrom selenium.common.exceptions import NoSuchElementExceptionfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support.ui import WebDriverWaitimport time ,sys, requests, jsonclass Browser(object):def __init__(self,url): self.browser = webdriver.Chrome() self.browser.maximize_window() self.url = url self.browser.get(self.url)def get_element_by_id(self,element_id): return self.browser.find_element_by_id(element_id)def get_element_by_class(self,_class):return self.browser.find_element_by_class_name(_class)def get_element_by_text(self,text):return self.browser.find_element_by_link_text(text)def get_screenshot(self,file):return self.browser.get_screenshot_as_file(file) def waitfor(self,element_id):return WebDriverWait(self.browser,10).until(lambda b:b.find_element_by_id(element_id).is_displayed())def upload_code_check(): url = ':5678/api.php?method=upload'print url data = {} data.update({'username':'xxxxx'}) data.update({'password':'xxxxx'}) data.update({'codetype':'5000'}) data.update({'appid':'1'}) data.update({'appkey':'22cc5376925e9387a23cf797cb9ba745'}) data.update({'timeout':'60'}) data.update({'version':'YAPI/WEB v1.0.0'}) data.update({'showimage':'1'}) files = {'file':('dd.jpg',open('C://dd.jpg','rb'),'image/jpeg')} res = requests.post(url,data=data,files=files) res_json = json.loads(res.content)print res.contentreturn res_json def get_code_check_results(url1):while True: res1 = requests.get(url1) res1 = json.loads(res1.content)if res1['ret'] == 0:breakreturn res1def get_image(img_url): img = requests.get(img_url) img_f = open('C://dd.jpg','w+b') img_f.write(img.content) img_f.close()return img def main(): url = ''count = 'xxxxxxx'pwd = 'xxxxxxxx'pwd_new = 'xxxxxxx'#email_new = 'xxxxx@qq.com'email_new = 'xxxxxxxx@qq.com'browser = Browser(url)if browser.waitfor('nav-link-yourAccount'): browser.get_element_by_id('nav-link-yourAccount').click()else:print 'connect timeout'sys.exit(1)if browser.waitfor('ap_email'): browser.get_element_by_id('ap_email').send_keys(count) browser.get_element_by_id('ap_password').send_keys(pwd) browser.get_element_by_id('signInSubmit').click()try: error = browser.get_element_by_id('auth-error-message-box')print 'login error'except Exception:passelse:print 'connect timeout'sys.exit(1)if browser.waitfor('nav-link-yourAccount'): browser.get_element_by_id('nav-link-yourAccount').click()else:print 'connect timeout'sys.exit(1)try: text = browser.get_element_by_text(u'登录和安全设置') text.click() email = browser.get_element_by_id('auth-cnep-edit-email-button') email.click()if browser.waitfor('ap_email_new'): browser.get_element_by_id('ap_email_new').send_keys(email_new) browser.get_element_by_id('ap_email_new_check').send_keys(email_new) browser.get_element_by_id('ap_password').send_keys(pwd) yanzhenma = browser.get_element_by_id('auth-captcha-image') img_url = yanzhenma.get_attribute('src') img_url = img_url.replace('&','&')print img_url img = get_image(img_url) res_json = upload_code_check() url1 = ":5678/api.php?method=result&cid=" + str(res_json['cid'])print url1 res1 = get_code_check_results(url1)print res1['text'] yzm = res1['text']print yzmif yzm: browser.get_element_by_id('auth-captcha-guess').send_keys(str(yzm)) browser.get_element_by_id('cnep_1B_submit_button').click()if browser.waitfor('auth-success-message-box'): browser.get_element_by_id('auth-cnep-edit-password-button').click()print 'success modify email'if browser.waitfor('ap_password_new_check'): browser.get_element_by_id('ap_password').send_keys(pwd) browser.get_element_by_id('ap_password_new').send_keys(pwd_new) browser.get_element_by_id('ap_password_new_check').send_keys(pwd_new) auth_img_url = browser.get_element_by_id('auth-captcha-image') img_url2 = auth_img_url.get_attribute('src').replace('&','&') img2 = get_image(img_url2) res_json = upload_code_check() url2 = ":5678/api.php?method=result&cid=" + str(res_json['cid'])print url2 res2 = get_code_check_results(url2)print res2['text'] yzm2 = res2['text']print yzm2if yzm2: browser.get_element_by_id('auth-captcha-guess').send_keys(str(yzm2)) browser.get_element_by_id('cnep_1D_submit_button').click() except NoSuchElementException,e:print(e) if __name__ == "__main__": main()
Atas ialah kandungan terperinci selenium之实现自动登录的实例代码. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!