forked from LikeToAccess/TMF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_captcha.py
69 lines (63 loc) · 2.19 KB
/
find_captcha.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# -*- coding: utf-8 -*-
# filename : find_captcha.py
# description : Class containing methods for finding elements
# author : Ian Ault
# email : [email protected]
# date : 05-19-2022
# version : v1.0
# usage : python main.py
# notes : This file should not be run directly
# license : MIT
# py version : 3.10.2
#==============================================================================
import os
import time
from selenium.common.exceptions import *
from selenium.webdriver.common.by import By
from wait_until_element import Wait_Until_Element
from find_element import Find_Element
class Find_Captcha(Find_Element, Wait_Until_Element):
def check_captcha(self):
# "Myles" - Myles
# "Liam" - Liam
try:
captcha_image = self.wait_until_element(
By.XPATH,
"//*[@id=\"checkcapchamodelyii-captcha-image\"]",
timeout=1.5
)
captcha_input = self.driver.find_element(By.XPATH, "//*[@id=\"checkcapchamodelyii-captcha\"]")
captcha_submit = self.driver.find_element(By.XPATH, "//*[@id=\"player-captcha\"]/div[3]/div/div")
except TimeoutException:
return False
return captcha_image, captcha_input, captcha_submit
def get_captcha_image(self):
captcha = self.check_captcha()
if captcha:
print("\tWARNING: Found captcha!")
captcha_image, *_ = captcha
timeout = time.time()
print("\tWaiting for captcha image to load...")
while captcha_image.size["width"] == 0 or time.time() - timeout < 5:
time.sleep(0.25)
# self.driver.save_screenshot("screenshot.png")
captcha_image.screenshot("captcha.png")
print("\tCaptcha image saved.")
return 225
return 200
def resolve_captcha(self):
captcha = self.check_captcha()
while captcha:
captcha_image, captcha_input, captcha_submit = captcha
print("\tWARNING: Found captcha!")
try:
captcha_image.screenshot("captcha.png")
captcha_input.send_keys(input("\t\tSolve Captcha:\n\t\t> "))
captcha_submit.click()
except WebDriverException:
os.remove("captcha.png")
break
self.wait_until_element(
By.TAG_NAME, "video", timeout=3
).get_attribute("src")
captcha = self.check_captcha()