Created
January 6, 2018 11:49
-
-
Save leavyli/a62bcd5445d76f5f9ceb142f555d73ea to your computer and use it in GitHub Desktop.
python:selenium:verity_by_slide:(一个简单的通过selenium控制滑动验证的例子,网站可能未必能用)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from selenium import webdriver | |
| from selenium.webdriver import ActionChains | |
| from selenium.common.exceptions import NoSuchElementException | |
| import time | |
| import os | |
| import base64 | |
| import re | |
| import uuid | |
| # 初始化chrome的dirver路径 | |
| dirver_path = r"D:\chromedriver_win32\chromedriver.exe" | |
| driver = webdriver.Chrome(dirver_path) | |
| ### test | |
| def start(): | |
| # | |
| phone = "13229577842" | |
| test_url = r"https://www.panda.tv/resetpwd#write" | |
| driver.delete_all_cookies() | |
| driver.refresh() | |
| driver.get(test_url) | |
| input_element = driver.find_element_by_class_name("writeaccount-account") | |
| input_element.send_keys(phone) | |
| next_button = driver.find_element_by_class_name("next-btn") | |
| next_button.click() | |
| # 简单的滑动动作 | |
| c = 0 | |
| while ( c < 100): | |
| try: | |
| # slide = driver.find_element_by_xpath('//*[@id="nc_{c}_n1z"]'.format(c=c)) | |
| slide = driver.find_element_by_xpath('//*[@class="nc_iconfont btn_slide"]') | |
| break | |
| except (NoSuchElementException): | |
| c += 1 | |
| time.sleep(2) | |
| continue | |
| time.sleep(5) | |
| action = ActionChains(driver) | |
| action.click_and_hold(slide) | |
| action.move_by_offset(300, 0) | |
| action.release().perform() | |
| # 查找验证的图片 | |
| cnt_stop = 10 | |
| while ( cnt_stop > 0): | |
| try: | |
| captcha_img = driver.find_element_by_class_name("clickCaptcha_img") | |
| c = captcha_img.find_elements_by_tag_name("img") | |
| im = c[0] | |
| base64_data = im.get_attribute('src')[len(r"data:image/jpg;base64,"):] | |
| break | |
| except : | |
| time.sleep(2) | |
| cnt_stop -= 1 | |
| continue | |
| try: | |
| img_data = base64.b64decode(base64_data) | |
| #需要点击的字 | |
| click_text = driver.find_elements_by_xpath('//div[@class="scale_text slidetounlock scale_text2"]')[0] | |
| regx = r'“(.)”' | |
| r = re.compile(regx) | |
| chart = r.findall(click_text.text)[0] | |
| t = uuid.uuid4() | |
| title = t.hex +"_" + chart | |
| save_img = r"e:/test/xiongmao/{title}.jpg".format(title=title) | |
| with open(save_img, "wb") as f: | |
| f.write(img_data) | |
| except: | |
| pass | |
| # for i in range(200): | |
| # start() | |
| start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment