Selenium example code to implement automatic login

Source: Internet
Author: User
Use selenium and coding platform (oneself also can not achieve verification code image resolution, docking code platform resolution image verification code) reality automatically login to the Amazon website, and modify the account binding mailbox and password and other operations.

Logic: Simulate the operation of the browser, positioning elements fill in the data, according to the page elements get properties download image verification code for analysis, automatically fill the verification code, to achieve automated operation.

To use the Chrome browser, you need to download the corresponding version of the Webdriver download address:.

#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):d EF __init__ (self,url): Self.browser = Webdriver. Chrome () Self.browser.maximize_window () Self.url = URL Self.browser.get (self.url) def get_element_by_i D (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 ': ' The '} ') data.update ({' Appi d ': ' 1 '}) data.update ({' Appkey ': ' 22cc5376925e9387a23cf797cb9ba745 '}) data.update ({' Timeout ': '} ') data.update ({' V Ersion ': ' 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 re S_json def get_code_check_results (URL1): While true:res1 = Requests.get (url1) res1 = Json.loads (Res1.con Tent) If res1[' ret '] = = 0:breakreturn res1def get_image (img_url): img = Requests.get (img_url) img_f = open (' c://dd.jp G ', ' 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.W Aitfor (' Ap_email '): browser.get_element_by_id (' Ap_email '). Send_keys (count) browser.get_element_by_id (' Ap_pas Sword '). 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 ' Login and security settings ') Text.click () email = b            rowser.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-capt Cha-image ') Img_url = Yanzhenma.get_attribute (' src ') img_url = Img_url.replace (' & ', ' & ') p Rint 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-SUCC  Ess-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) r Es_json = Upload_code_check () url2 = ": 5678/api.php?method=result&cid=" + str (res_json[' CID ')) Print URL 2 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_e lement_by_id (' Cnep_1d_submit_button '). Click () except Nosuchelementexception,e:print (e) if __name__ = = "__ma In__ ": Main ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.