python selenium浏览器代理提交

要下载对应版本的chromedriver ,   图形验证码多个点顺序点击好难,放弃了

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import os,sys,time,uuid,random
from PIL import Image

class Hdldriver(object):
    def __init__(self, url, prt, time2wait=10):
        ex_path = 'C:\Program Files\Google\Chrome\Application\chromedriver.exe'
        chrome_options = Options()
        #chrome_options.add_argument("--proxy-server=http://%s" % prt)
        aglst = ['Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1',
                 'Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1',
                 'Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B176 MicroMessenger/4.3.2',
                 'Mozilla/5.0 (Linux; U; Android 4.3; zh-cn; SM-N9008 Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
                 'Mozilla/5.0 (Linux; U; Android 4.3; zh-cn; SCHlike Gecko) Version/4.0 Mobile Safari/534.30',
                 'Mozilla/5.0 (Linux; U; Android 4.2.2; zh-cn; DKL01 Build/DKL01) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
                 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_3 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Mobile/10B329',
                 'Mozilla/5.0 (Linux; U; Android 2.3.6; zh-cn; GT-I9228 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1']
        agent = random.choice(aglst)
        chrome_options.add_argument('user-agent="%s"' % agent)
        chrome_options.add_argument('disable-infobars')
        self.browser = webdriver.Chrome(executable_path= ex_path, chrome_options=chrome_options)
        self.browser.delete_all_cookies()
        self.browser.set_window_size(430,800)
        self.browser.implicitly_wait(10)
        self.browser.get(url)
        self.wait = WebDriverWait(self.browser, time2wait)

    def __basePath(self, dbpath):
        base_path = os.path.realpath(sys.path[0])
        filepath = os.path.join(base_path, dbpath)
        if filepath.find("base_library.zip") > 0:
            filepath = filepath.replace("base_library.zip\\", "")
        return filepath

    def __inputUserinfo(self, username, password):
        _input = self.wait.until(EC.presence_of_element_located((By.XPATH, "/html/body/div/div[1]/div/div/form/div[1]/input")))
        _input.send_keys(username)
        _input = self.wait.until(EC.presence_of_element_located((By.XPATH, "/html/body/div/div[1]/div/div/form/div[2]/input")))
        _input.send_keys(password)

    def __clickRegisterbtn(self):
        _btn = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "btn-primary")))
        _btn.click()

    def __corpPageImage(self, _did):
        self.browser.switch_to.default_content()
        try:
            self.wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'captcha_widget'+_did)))
            _btn = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "captcha-widget-text")))
            _btn.click()
            time.sleep(2)
            oldpath = self.__basePath('logs/' + str(uuid.uuid1()) + '.png')
            self.browser.save_screenshot(oldpath)
            self.browser.switch_to.default_content()
            img = self.wait.until(EC.presence_of_element_located((By.ID, 'l-captcha-image'+_did)))
            left, top = img.location['x'], img.location['y']
            width, height = 300 + left, 160 + top
            oldpic = Image.open(oldpath)
            newpic = oldpic.crop((left, top, width, height))
            picpath = self.__basePath('logs/' + str(uuid.uuid1()) + '.png')
            newpic.save(picpath)
            os.remove(oldpath)
            return picpath
        except Exception as e:
            pass

    def __slideVerifyCode(self, _did):
        picpath = self.__corpPageImage(_did)
        if picpath == '':
            return False
        #x, y = moments_img(picpath)
        x, y = 100, 35
        self.browser.execute_script("document.getElementById('l-captcha-overlay"+_did+"').style.display='none'")
        self.wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'captcha_frame' + _did)))
        ActionChains(self.browser).move_to_element_with_offset(self.wait.until(EC.presence_of_element_located((By.ID, 'lc-image-panel'))), x, y).click().perform()
        self.browser.switch_to.default_content()
        #os.remove(picpath)
        return True
        try:
            EC.text_to_be_present_in_element((By.CLASS_NAME, 'captcha-widget-text'), r'恭喜,验证成功')
            print('验证成功!')
            return True
        except Exception as e:
            return False

    def verifySlideCode(self,attempt_times=10):
        self.wait.until(EC.presence_of_element_located((By.ID, 'lc-captcha-response')))
        _div = self.browser.find_element_by_id('lc-captcha-response')
        _did = _div.get_attribute('data-id')
        for attempt in range(attempt_times):
            try:
                if self.__slideVerifyCode(_did):
                    return True
            except Exception as e:
                print(e)
                time.sleep(1)
        return False


    def simSubmitReg(self, username, password):
        self.__inputUserinfo(username, password)
        if self.verifySlideCode():
            self.__clickRegisterbtn()
        else:
            self.browser.quit()


if __name__ == '__main__':
    drv = Hdldriver('https://captcha.luosimao.com/demo/', '59.32.37.94')
    drv.simSubmitReg('test', '123456')

 

posted @ 2019-05-07 12:43  1553  阅读(730)  评论(0编辑  收藏  举报