Home > OS >  Selenium : switch to modal window handle gives error
Selenium : switch to modal window handle gives error

Time:08-14

I have this webpage enter image description here

I ttried checking the window handles and switching to them one by one which gives me

driver.window_handles
['CDwindow-34B695696D2295F87F84F06321D10117', 'CDwindow-212C47AEC0CCD8240A4F9675D5B5BEF2', 'CDwindow-5A39DFE9B9C75CFA7316BF2098765D05', 'CDwindow-796B9EF6777039A036CCF7C3D452807A', 'CDwindow-1DF355426CF56902DC339955FF55C9AE', 'CDwindow-1B700B499C716086FA269E89B0ED3835']
for handle in driver.window_handles:
    driver.switch_to.window(handle)
    try:
        checkbox = driver.find_element(By.XPATH,'//div[@id="checkbox"]')
        print('found')
    except:
        pass

but I get the same error "No such element"

Talking about solving the captch : I have an external API that does it for me, but I need to click that check box here, but stuck in switching to the modal part

Note that : to reproduce issue you can use same code above, no need to create account.

CodePudding user response:

Problem is that you have 2 nested iframes in the site, You'll have to perform driver.switch_to.frame() for each of them:

# After pressing "sign-in"
captcha_iframe = driver.find_element(By.CSS_SELECTOR, '[title="Captcha"]')
driver.switch_to.frame(captcha_iframe)
inner_iframe = driver.find_element(By.CSS_SELECTOR, 'iframe')
driver.switch_to.frame(inner_iframe)
# Perform captcha check
driver.find_element(By.CSS_SELECTOR, '#checkbox').click()

Notice that after each switch_to, I directly use the new driver's context, and I do not perform a search under the element I found.

  • Related