use shadow roots and fix HTTP request failed: [500:InternalServerError] #2

This commit is contained in:
Thephaseless 2024-08-25 15:24:27 +00:00
parent bbb05b255f
commit 55b241a0cc

View File

@ -1,4 +1,5 @@
import asyncio import asyncio
from typing import List
import nodriver as webdriver import nodriver as webdriver
from nodriver.core.element import Element from nodriver.core.element import Element
@ -70,29 +71,54 @@ async def bypass_cloudflare(page: webdriver.Tab):
"Verify you are human by completing the action below.", "Verify you are human by completing the action below.",
timeout=3, timeout=3,
) )
# If challenge solves by itself
except asyncio.TimeoutError: except asyncio.TimeoutError:
if page.target.title not in CHALLENGE_TITLES: if page.target.title not in CHALLENGE_TITLES:
return challenged return challenged
raise raise
logger.debug("Timed out waiting for element, trying again")
if elem is None: if elem is None:
logger.debug("Couldn't find the title, trying again")
continue continue
elem = elem.parent elem = elem.parent
for _ in range(3): # Get the element containing the shadow root
for _ in range(4):
if elem is not None: if elem is not None:
elem = await elem.query_selector("div") # type: ignore reportAttributeAccessIssue elem = get_first_div(elem)
else: else:
raise InvalidElementError raise InvalidElementError
continue if isinstance(elem, Element) and elem.shadow_roots:
if isinstance(elem, Element): inner_elem = Element(elem.shadow_roots[0], page, elem.tree).children[0]
if isinstance(inner_elem, Element):
logger.debug("Clicking element") logger.debug("Clicking element")
await elem.mouse_click() await inner_elem.mouse_click()
else:
logger.warn(
"Element is a string, please report this to Byparr dev"
) # I really hope this never happens
else: else:
logger.warn("Coulnd't find checkbox, trying again...") logger.warn("Coulnd't find checkbox, trying again...")
def get_first_div(elem):
"""
Retrieve the first div element from the given element's children.
Args:
----
elem: The parent element to search for a div child.
Returns:
-------
The first div element found, or the original element if no div is found.
"""
if isinstance(elem.children, List):
elem = next(x for x in elem.children if x.tag_name == "div")
return elem
class InvalidElementError(Exception): class InvalidElementError(Exception):
pass pass