Advertisement
Guest User

Untitled

a guest
Jun 9th, 2017
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. # Imagetyperz captcha API test
  2. # ------------------------------
  3. from imagetypersapi import ImageTypersAPI
  4. from time import sleep
  5. from selenium import webdriver
  6. from PIL import Image
  7.  
  8. def screenshot_captcha(driver):
  9. element = driver.find_element_by_name('verificationCode') # get it again here
  10. location = element.location
  11. size = element.size
  12. driver.save_screenshot('screenshot.png') # saves screenshot of entire page
  13.  
  14. im = Image.open('screenshot.png') # uses PIL library to open image in memory
  15.  
  16. left = location['x']
  17. top = location['y']
  18. right = location['x'] + size['width']
  19. bottom = location['y'] + size['height']
  20.  
  21.  
  22. im = im.crop((left, top, right, bottom)) # defines crop points
  23. im.save('captcha.png') # saves new cropped image
  24.  
  25. # solve captcha
  26. def test_api():
  27. web_user = 'abc'
  28. web_pass = 'def'
  29.  
  30. username = 'testingfor'
  31. password = 'testingfor'
  32. driver = webdriver.Chrome()
  33. print 'going to page'
  34. driver.get('https://www.chinesean.com/affiliate/index.do') # go to link
  35.  
  36. # complete user and pass
  37. print 'typing credentials'
  38. driver.find_element_by_name('username').send_keys(web_user)
  39. driver.find_element_by_name('password').send_keys(web_pass)
  40.  
  41. # get captcha image
  42. print 'saving captcha'
  43. captcha_field = driver.find_element_by_name('verificationCode')
  44. screenshot_captcha(driver) # screenshot
  45.  
  46. print 'solving captcha'
  47. # init captcha api
  48. ita = ImageTypersAPI(username, password) # init imagetyperz api obj
  49. captcha_text = ita.solve_captcha('captcha.png', case_sensitive = True) # solve captcha, with case sensitive arg True
  50.  
  51. # submit captcha
  52. captcha_field.send_keys(captcha_text)
  53. captcha_field.submit()
  54.  
  55. sleep(30)
  56. driver.quit()
  57.  
  58. # main method
  59. def main():
  60. try:
  61. test_api() # test captcha API
  62. except Exception, ex:
  63. print '[!] Error occured: {}'.format(ex)
  64.  
  65. if __name__ == "__main__":
  66. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement