Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Imagetyperz captcha API test
- # ------------------------------
- from imagetypersapi import ImageTypersAPI
- from time import sleep
- from selenium import webdriver
- from PIL import Image
- def screenshot_captcha(driver):
- element = driver.find_element_by_name('verificationCode') # get it again here
- location = element.location
- size = element.size
- driver.save_screenshot('screenshot.png') # saves screenshot of entire page
- im = Image.open('screenshot.png') # uses PIL library to open image in memory
- left = location['x']
- top = location['y']
- right = location['x'] + size['width']
- bottom = location['y'] + size['height']
- im = im.crop((left, top, right, bottom)) # defines crop points
- im.save('captcha.png') # saves new cropped image
- # solve captcha
- def test_api():
- web_user = 'abc'
- web_pass = 'def'
- username = 'testingfor'
- password = 'testingfor'
- driver = webdriver.Chrome()
- print 'going to page'
- driver.get('https://www.chinesean.com/affiliate/index.do') # go to link
- # complete user and pass
- print 'typing credentials'
- driver.find_element_by_name('username').send_keys(web_user)
- driver.find_element_by_name('password').send_keys(web_pass)
- # get captcha image
- print 'saving captcha'
- captcha_field = driver.find_element_by_name('verificationCode')
- screenshot_captcha(driver) # screenshot
- print 'solving captcha'
- # init captcha api
- ita = ImageTypersAPI(username, password) # init imagetyperz api obj
- captcha_text = ita.solve_captcha('captcha.png', case_sensitive = True) # solve captcha, with case sensitive arg True
- # submit captcha
- captcha_field.send_keys(captcha_text)
- captcha_field.submit()
- sleep(30)
- driver.quit()
- # main method
- def main():
- try:
- test_api() # test captcha API
- except Exception, ex:
- print '[!] Error occured: {}'.format(ex)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement