Skip to content

Commit

Permalink
Button locator algorithm with a recursive search feature
Browse files Browse the repository at this point in the history
Now it can dynamically find the location of the button.
  • Loading branch information
sarperavci committed Aug 14, 2024
1 parent b44c941 commit 1845e19
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
54 changes: 51 additions & 3 deletions CloudflareBypasser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,64 @@ def __init__(self, driver: ChromiumPage, max_retries=-1, log=True):
self.driver = driver
self.max_retries = max_retries
self.log = log


def search_recursively_shadow_root_with_iframe(self,ele):
if ele.shadow_root:
if ele.shadow_root.child().tag == "iframe":
return ele.shadow_root.child()
else:
for child in ele.children():
result = self.search_recursively_shadow_root_with_iframe(child)
if result:
return result
return None

def search_recursively_shadow_root_with_cf_input(self,ele):
if ele.shadow_root:
if ele.shadow_root.ele("tag:input"):
return ele.shadow_root.ele("tag:input")
else:
for child in ele.children():
result = self.search_recursively_shadow_root_with_cf_input(child)
if result:
return result
return None

def locate_cf_button(self):
button = None
eles = self.driver.eles("tag:input")
for ele in eles:
if "name" in ele.attrs.keys() and "type" in ele.attrs.keys():
if "turnstile" in ele.attrs["name"] and ele.attrs["type"] == "hidden":
button = ele.parent().shadow_root.child()("tag:body").shadow_root("tag:input")
break

if button:
return button
else:
# If the button is not found, search it recursively
self.log_message("Basic search failed. Searching for button recursively.")
ele = self.driver.ele("tag:body")
iframe = self.search_recursively_shadow_root_with_iframe(ele)
if iframe:
button = self.search_recursively_shadow_root_with_cf_input(iframe("tag:body"))
else:
self.log_message("Iframe not found. Button search failed.")
return button

def log_message(self, message):
if self.log:
print(message)

def click_verification_button(self):
try:
if self.driver.wait.ele_displayed('#GBddK6', timeout=1.5):
button = self.locate_cf_button()
if button:
self.log_message("Verification button found. Attempting to click.")
self.driver.ele("#GBddK6", timeout=2.5).click()
button.click()
else:
self.log_message("Verification button not found.")

except Exception as e:
self.log_message(f"Error clicking verification button: {e}")

Expand All @@ -31,6 +78,7 @@ def is_bypassed(self):
def bypass(self):

try_count = 0
self.click_verification_button() # Click the button first to avoid false positive of is_bypassed()

while not self.is_bypassed():
if 0 < self.max_retries + 1 <= try_count:
Expand Down
3 changes: 2 additions & 1 deletion test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def get_chromium_options(browser_path: str, arguments: list) -> ChromiumOptions:
:return: Configured ChromiumOptions instance.
"""
options = ChromiumOptions()
options.set_argument('--auto-open-devtools-for-tabs', 'true')
options.set_argument('--auto-open-devtools-for-tabs', 'true') # we don't need this anymore
options.set_paths(browser_path=browser_path)
for argument in arguments:
options.set_argument(argument)
Expand Down Expand Up @@ -63,6 +63,7 @@ def main():
# Where the bypass starts
logging.info('Starting Cloudflare bypass.')
cf_bypasser = CloudflareBypasser(driver)
# time.sleep(3) # If the page is slow to load, you can add a delay here.
cf_bypasser.bypass()

logging.info("Enjoy the content!")
Expand Down

0 comments on commit 1845e19

Please sign in to comment.