[Python] How to retrieve token value from iframe using Python Playwright?

Discussão em 'Python' iniciado por Stack, Novembro 26, 2024.

  1. Stack

    Stack Membro Participativo

    I'm working on a script using Python Playwright to retrieve token on a webpage. I'm able to locate the iframe that contains it, but I'm struggling to extract the token from the iframe, which is stored in the data-token-response attribute after the challenge is solved.

    Note: here challenge is specific steps that i would have to solve, then token gets populated in data-token-response field

    Here's what I have so far:

    1. I locate the iframe that contains the challenge using the following selector:

    TOKEN_IFRAME: str = "(//iframe[contains(@title,'Widget')])[1]"
    self.checkbox_frame = self.page.locator(self.variable.TOKEN_IFRAME)


    1. I attempt to extract the token from the data-token-response attribute.
    2. My current code for retrieving the token is:

    async def get_token(self):
    iframe_element = self.page.locator(self.variable.TOKEN_IFRAME)
    token = await iframe_element.get_attribute("data-token-response")
    print(token, await iframe_element.get_attribute("style")) # we do get the style property of the iframe. But the token prints None.
    if captcha_token:
    _print(f"token: {token}")
    else:
    _print("CAPTCHA token not found or not yet solved.")
    return token


    What I've tried:

    • Debugging the iframe's other attributes, which works fine for style but always returns None for the data-token-response.
    • Verifying that the iframe and its content are fully loaded.

    Continue reading...

Compartilhe esta Página