Python [Automated] selenium: A Preliminary Study of realizing automatic login to Amazon for operations, pythonselenium
You can use selenium and CAPTCHA human bypass platforms (you cannot parse Verification Code images and connect them to CAPTCHA human bypass platforms) to automatically log on to the Amazon website and change your account's email address and password.
Logic: Simulate browser operations, fill in data with positioning elements, download the image verification code based on the page elements for analysis, and automatically fill the verification code for automated operations.
To use the chrome browser, download the corresponding version of webdriver: https://chromedriver.storage.googleapis.com/index.html.
# 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_e Lement_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 = 'HTTP: // api.yundama.net: 5678/api. php? Method = upload 'print url data = {} data. update ({'username': 'xxxxx'}) data. update ({'Password': 'xxxxx'}) data. update ({'codetype ': '000000'}) 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 = requ Ests. post (url, data = data, files = files) res_json = json. loads (res. content) print res. content return res_json def get_code_check_results (url1): while True: res1 = requests. get (url1) res1 = json. loads (res1.content) if res1 ['ret '] = 0: break return 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 = 'Https: // www.amazon.cn/'count = 'xxxxxxxx' pwd = 'xxxxxxxx' pwd_new = 'xxxxxxxx' # 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' failed t Exception: pass else: 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: te Xt = browser. get_element_by_text (u'logon and Security settings') text. click () email = browser. get_element_by_id ('auth-nningp-edit-email-link') 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 ('& amp;', '&') print img_url img = get_image (img_url) res_json = upload_code_check () url1 = "http://api.yundama.net: 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 yzm if yzm: browser. get_element_by_id ('auth-captcha-guess '). send_keys (str (yzm) browser. get_element_by_id ('nginx' _ 1B_submit_button '). click () if browser. waitfor ('auth-success-message-box'): browser. get_element_by_id ('auth-nningp-edit-password-click '). click () print 'succe Ss 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 ('& amp;', '&') img2 = get_image (im G_url2) res_json = upload_code_check () url2 = "http://api.yundama.net: 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 yzm2 if yzm2: browser. get_element_by_id ('auth-captcha-guess '). send_keys (str (yzm2) browser. get_element_by_id ('nginx' _ 1D_submit_button '). click () Cancel t NoSuchElementException, e: print (e) if _ name _ = "_ main _": main ()