Interact with a button inside an iframe using the nodriver library in Python

97 Views Asked by At

I'm working on an automated testing project in Python and using a library called nodriver. Currently, I'm facing difficulties selecting and clicking a button within an iframe, specifically a reCAPTCHA captcha.

Here's a snippet of the function I'm trying to implement:

async def click_captcha(tab):
    # Trouver l'iframe du reCAPTCHA
    iframe = await tab.select('iframe[title="reCAPTCHA"]') #It works
    print("IFRAME............................;", iframe)
    tab.switch_to.frame(iframe)  # Here is the error
    button_in_iframe = await tab.select('span[id="recaptcha-anchor"]')
    await button_in_iframe.click()
    return tab

async def main():
    driver = await uc.start()
    tab = await driver.get("https://mysuper-website.com")
    tab = await click_captcha(tab)

I'm receiving an error indicating the Tab object does not have an switch_to attribute. How can I properly switch context to the iframe and interact with the button inside it using nodriver? Is there a specific method for handling iframes with this library? The library: https://github.com/ultrafunkamsterdam/nodriver

I tried to search inside the code of the lib but I didn't find anything interesting

1

There are 1 best solutions below

0
Divyanshu Tiwari On

Using nodriver, you don't need to switch to iframe to interact with it. nodriver's search methods automatically switch to iframe if the searched text/element is not found on the parent document or if you are using multiple elements selectors.

From nodriver's README:

smart element lookup, by selector or text, including iframe content. this could also be used as wait condition for a element to appear, since it will retry for the duration of until found. single element lookup by text using tab.find(), accepts a best_match flag, which will not naively return the first match, but will match candidates by closest matching text length.

Though currently, this is not working as intended. As per my debugging, nodriver does try to find elements in iframes but iframes are not returning any elements within it so it's impossible to interact with any element inside it. The repo owner (Leon aka. ultrafunkamsterdam) has not yet completed the repo so this should get fixed in future versions. Best you can do is wait.