Merge 4bcf1bb637dce1b67b477bf55ac0de1150a0c567 into ce5369dd413cd71a81ce38a5ccd379f6c9352e23

This commit is contained in:
MCG-pok 2025-03-03 23:49:06 -05:00 committed by GitHub
commit 9d5c412da9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -219,6 +219,20 @@ def _cmd_sessions_destroy(req: V1RequestBase) -> V1ResponseBase:
"message": "The session has been removed." "message": "The session has been removed."
}) })
def _init_driver(driver):
try:
driver.execute_cdp_cmd('Page.enable', {})
driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
'source': """
Element.prototype._as = Element.prototype.attachShadow;
Element.prototype.attachShadow = function (params) {
return this._as({mode: "open"})
};
"""
})
except Exception as e:
logging.debug("Driver init exception: %s", repr(e))
def _resolve_challenge(req: V1RequestBase, method: str) -> ChallengeResolutionT: def _resolve_challenge(req: V1RequestBase, method: str) -> ChallengeResolutionT:
timeout = int(req.maxTimeout) / 1000 timeout = int(req.maxTimeout) / 1000
@ -239,6 +253,7 @@ def _resolve_challenge(req: V1RequestBase, method: str) -> ChallengeResolutionT:
else: else:
driver = utils.get_webdriver(req.proxy) driver = utils.get_webdriver(req.proxy)
logging.debug('New instance of webdriver has been created to perform the request') logging.debug('New instance of webdriver has been created to perform the request')
_init_driver(driver)
return func_timeout(timeout, _evil_logic, (req, driver, method)) return func_timeout(timeout, _evil_logic, (req, driver, method))
except FunctionTimedOut: except FunctionTimedOut:
raise Exception(f'Error solving the challenge. Timeout after {timeout} seconds.') raise Exception(f'Error solving the challenge. Timeout after {timeout} seconds.')
@ -252,23 +267,33 @@ def _resolve_challenge(req: V1RequestBase, method: str) -> ChallengeResolutionT:
logging.debug('A used instance of webdriver has been destroyed') logging.debug('A used instance of webdriver has been destroyed')
def get_shadowed_iframe(driver: WebDriver, css_selector: str):
logging.debug("Getting ShadowRoot by selector: %s", css_selector)
shadow_element = driver.execute_script("""
return document.querySelector(arguments[0]).shadowRoot.firstChild;
""", css_selector)
if shadow_element:
logging.debug("iframe found")
else:
logging.debug("iframe not found")
return shadow_element
def click_verify(driver: WebDriver): def click_verify(driver: WebDriver):
try: try:
logging.debug("Try to find the Cloudflare verify checkbox...") logging.debug("Try to find the Cloudflare verify checkbox...")
iframe = driver.find_element(By.XPATH, "//iframe[starts-with(@id, 'cf-chl-widget-')]") iframe = get_shadowed_iframe(driver, "div:not(:has(div))")
driver.switch_to.frame(iframe) driver.switch_to.frame(iframe)
checkbox = driver.find_element( iframe_body = driver.find_element(By.CSS_SELECTOR, "body")
by=By.XPATH, if iframe_body:
value='//*[@id="content"]/div/div/label/input', iframe_body.click()
)
if checkbox:
actions = ActionChains(driver) actions = ActionChains(driver)
actions.move_to_element_with_offset(checkbox, 5, 7) actions.move_to_element_with_offset(iframe_body, 10, 10)
actions.click(checkbox) actions.click(iframe_body)
actions.perform() actions.perform()
logging.debug("Cloudflare verify checkbox found and clicked!") logging.debug("Attempted to click on iframe body")
except Exception: except Exception as e:
logging.debug("Cloudflare verify checkbox not found on the page.") logging.debug("Cloudflare verify checkbox not found on the page. %s", repr(e))
finally: finally:
driver.switch_to.default_content() driver.switch_to.default_content()